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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.