Flynnwood
Established Member
"Search" (in this case) is about how MySQL is constructed (it is a relational database) and about how tables / cols are assigned importance / relationships based on user queries.
Example:
INSERT INTO books VALUES (1,'First Book',NULL);
INSERT INTO books VALUES (2,'Second Book',NULL);
INSERT INTO books VALUES (3,'Third Book',NULL);
INSERT INTO keywords VALUES (1,'keyword1',NULL);
INSERT INTO keywords VALUES (2,'keyword2',NULL);
INSERT INTO keywords VALUES (3,'keyword3',NULL);
INSERT INTO book_keywords VALUES (1,1,1);
INSERT INTO book_keywords VALUES (2,2,1);
INSERT INTO book_keywords VALUES (3,2,2);
INSERT INTO book_keywords VALUES (4,3,1);
INSERT INTO book_keywords VALUES (5,3,2);
INSERT INTO book_keywords VALUES (6,3,3);
mysql> select * from books;
+---------+-------------+-----------+
| book_id | title | sub_title |
+---------+-------------+-----------+
| 1 | First Book | NULL |
| 2 | Second Book | NULL |
| 3 | Third Book | NULL |
+---------+-------------+-----------+
3 rows in set (0.00 sec)
mysql> select * from keywords;
+------------+----------+-------+
| keyword_id | keyword | notes |
+------------+----------+-------+
| 1 | keyword1 | NULL |
| 2 | keyword2 | NULL |
| 3 | keyword3 | NULL |
+------------+----------+-------+
3 rows in set (0.00 sec)
mysql> select * from book_keywords;
+-----------------+---------+------------+
| book_keyword_id | book_id | keyword_id |
+-----------------+---------+------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 3 | 3 |
+-----------------+---------+------------+
6 rows in set (0.00 sec)
The following gets all books containing at least those two
keywords:
SELECT b.book_id, b.title
FROM books b,
book_keywords bk1,
book_keywords bk2,
keywords k1,
keywords k2
WHERE bk1.book_id = b.book_id
AND bk1.keyword_id = k1.keyword_id
AND k1.keyword = 'keyword1'
AND bk2.book_id = b.book_id
AND bk2.keyword_id = k2.keyword_id
AND k2.keyword = 'keyword2'
+---------+-------------+
| book_id | title |
+---------+-------------+
| 2 | Second Book |
| 3 | Third Book |
+---------+-------------+
2 rows in set (0.01 sec)
Just an example of what should be happening in 2014 on a commercial site ....
Example:
INSERT INTO books VALUES (1,'First Book',NULL);
INSERT INTO books VALUES (2,'Second Book',NULL);
INSERT INTO books VALUES (3,'Third Book',NULL);
INSERT INTO keywords VALUES (1,'keyword1',NULL);
INSERT INTO keywords VALUES (2,'keyword2',NULL);
INSERT INTO keywords VALUES (3,'keyword3',NULL);
INSERT INTO book_keywords VALUES (1,1,1);
INSERT INTO book_keywords VALUES (2,2,1);
INSERT INTO book_keywords VALUES (3,2,2);
INSERT INTO book_keywords VALUES (4,3,1);
INSERT INTO book_keywords VALUES (5,3,2);
INSERT INTO book_keywords VALUES (6,3,3);
mysql> select * from books;
+---------+-------------+-----------+
| book_id | title | sub_title |
+---------+-------------+-----------+
| 1 | First Book | NULL |
| 2 | Second Book | NULL |
| 3 | Third Book | NULL |
+---------+-------------+-----------+
3 rows in set (0.00 sec)
mysql> select * from keywords;
+------------+----------+-------+
| keyword_id | keyword | notes |
+------------+----------+-------+
| 1 | keyword1 | NULL |
| 2 | keyword2 | NULL |
| 3 | keyword3 | NULL |
+------------+----------+-------+
3 rows in set (0.00 sec)
mysql> select * from book_keywords;
+-----------------+---------+------------+
| book_keyword_id | book_id | keyword_id |
+-----------------+---------+------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 2 | 2 |
| 4 | 3 | 1 |
| 5 | 3 | 2 |
| 6 | 3 | 3 |
+-----------------+---------+------------+
6 rows in set (0.00 sec)
The following gets all books containing at least those two
keywords:
SELECT b.book_id, b.title
FROM books b,
book_keywords bk1,
book_keywords bk2,
keywords k1,
keywords k2
WHERE bk1.book_id = b.book_id
AND bk1.keyword_id = k1.keyword_id
AND k1.keyword = 'keyword1'
AND bk2.book_id = b.book_id
AND bk2.keyword_id = k2.keyword_id
AND k2.keyword = 'keyword2'
+---------+-------------+
| book_id | title |
+---------+-------------+
| 2 | Second Book |
| 3 | Third Book |
+---------+-------------+
2 rows in set (0.01 sec)
Just an example of what should be happening in 2014 on a commercial site ....