I am working on a tag system, what it should do is you can get the request and the selected tags, for example. jqueryand tags javascript, library. It should show only related scripts with the request AND only those that have tags. This is the database layout:
Script Table:
+-----------+---------------+
| script_id | name |
+-----------+---------------+
| 1 | jQuery |
| 2 | Sencha Touch |
| 3 | Codeigniter |
| 4 | Google Chrome |
| 5 | memcached |
| 6 | PHP |
| 7 | MooTools |
| 8 | node.js |
| 9 | jQuery Mobile |
+-----------+---------------+
Tag Table:
+--------+-------------+-------------+
| tag_id | name | url_name |
+--------+-------------+-------------+
| 1 | JavaScript | javascript |
| 2 | Library | library |
| 3 | PHP | php |
| 4 | MySQL | mysql |
| 5 | Cache | cache |
| 6 | HTML | html |
| 7 | Open source | open-source |
+--------+-------------+-------------+
Table with tags:
+-----------+--------+-----------+
| tagged_id | tag_id | script_id |
+-----------+--------+-----------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 1 | 9 |
+-----------+--------+-----------+
When I run my SQL, it still selects jQuery Mobile, but should not, because it does not contain a tag library, where jquery, I need it to limit the results that should match the selected tags.
This is my SQL:
SELECT scripts.script_id,
scripts.name
FROM
(
scripts scripts
LEFT OUTER JOIN tagged tagged ON tagged.script_id = scripts.script_id
LEFT OUTER JOIN tags tags ON tags.tag_id = tagged.tag_id
)
WHERE MATCH(scripts.name) AGAINST ('jquery*' IN BOOLEAN MODE) AND ( tags.url_name = 'javascript' OR tags.url_name = 'library' )
GROUP BY script_id
ORDER BY scripts.name
LIMIT 0, 25
It returns:
+-----------+---------------+
| script_id | name |
+-----------+---------------+
| 1 | jQuery |
| 9 | jQuery Mobile |
+-----------+---------------+
If I change ORto AND, it will not return anything or remove the brackets from the tags (and )it will not return anything either.
?