Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to run this query:

SELECT r.restaurant_name FROM restaurant as r, ethnicity as e, allergen as a WHERE e.restaurant_id=a.restaurant_id AND e.restaurant_id=r.restaurant_id AND a.restaurant_id=r.restaurant_id LIKE a.allergen='%fish%' LIKE e.ethnicity='%asian%';

If it helps this is the schema of all the tables in this query:

DESCRIBE ethnicity;
+---------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+-------+
| restaurant_id | int(11) | NO | MUL | NULL | |
| ethnicity | varchar(255) | NO | | NULL | |
+---------------+--------------+------+-----+---------+-------+

DESCRIBE restaurant;
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| restaurant_id | int(11) | NO | PRI | NULL | auto_increment |
| restaurant_name | varchar(255) | YES | | NULL | |
| phone_number | varchar(255) | YES | | NULL | |
| address_1 | varchar(255) | YES | | NULL | |
| address_2 | varchar(255) | YES | | NULL | |
| city | varchar(255) | YES | | NULL | |
| state | varchar(2) | YES | | NULL | |
| zip | int(11) | YES | | NULL | |
| filename | varchar(255) | YES | | NULL | |
| image | longblob | YES | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+

DESCRIBE allergen;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| allergen | varchar(255) | YES | | NULL | |
| restaurant_id | int(11) | YES | MUL | NULL | |
| menu_item_id | int(11) | YES | MUL | NULL | |
| allergen_id | int(11) | NO | PRI | NULL | auto_increment |
+---------------+--------------+------+-----+---------+----------------+

What I have tried:

I have tried running separate queries (broke the query into smaller parts) to find which part of this query is not working but every part works fine.

I looked up the error I am getting and what I found was this is usually given when unnecessary AND's are used or apostrophes are missing, only problem is that's not the case for my query.

Would appreciate the help of a seasoned or knowledgable person in MYSQL :)
Posted
Updated 30-Jun-20 0:26am
Comments
Richard MacCutchan 17-Mar-18 4:59am    
What is the type and value of the variable %asian%?

1 solution

You have random "LIKE"s in your query
D a.restaurant_id=r.restaurant_id LIKE a.allergen='%fish%' 

Try changing them to AND

That is also a very old fashioned way to join tables. See Visual Representation of SQL Joins[^] and Joining Tables in SQL[^]
E.g.
SQL
SELECT r.restaurant_name FROM #restaurant as r
LEFT JOIN #ethnicity as e ON e.restaurant_id=r.restaurant_id
LEFT JOIN #allergen as a ON a.restaurant_id=r.restaurant_id AND e.restaurant_id=a.restaurant_id
WHERE a.allergen='%fish%' and e.ethnicity='%asian%';
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900