Jump to content

help on query syntax


turpentyne

Recommended Posts

I want to search a database and get items that appear in the connector database twice with either variable 1 or 23. So if I search for any items that have been entered with both 1 and 23 variables, then I want to pull that item's id.

 

AND gives me nothing. OR gives me all the results of 1 and all the results of 23

 

item_id    |    variable

------------+----------------

211924 |    1

------------+----------------

211924 |    23

------------+----------------

 

This query below gives me no results, when I want to get 1 result: 211924

SELECT item_id FROM `connect_tables` JOIN items ON connect_tables.item_id = items.item_id

WHERE connect_tables.term_id LIKE 1  AND connect_tables.item_id LIKE 23

 

This query gives me every item that has been entered with 23 or 24, not with both.

SELECT item_id FROM `connect_tables` JOIN items ON connect_tables.item_id = items.item_id

WHERE connect_tables.term_id LIKE 1 OR connect_tables.item_id LIKE 23

 

I hope that made sense.

 

 

Link to comment
https://forums.phpfreaks.com/topic/252176-help-on-query-syntax/
Share on other sites

Don't use "LIKE" when you mean "=" -- that's just confusing.

 

A valid can't be "1" and "23" at the same time -- that's why AND doesn't make any sense.

 

OR will give you either -- hence you're getting both.

 

If you want to find items that have 2 rows -- once with termId 1 and a second time with term_id 23 -- you'll need to join twice.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.