Jump to content

Recommended Posts

If I want to make this statement shorter:

SELECT id FROM table WHERE col = 'this' OR col = 'that' OR col = 'bla' OR col = 'blah'

 

I can do

SELECT id FROM table WHERE col IN('this','that','bla','blah')

 

Is there a clause similar to that for AND?

Or I must I do:

SELECT id FROM table WHERE col = 'this' AND col = 'that' AND col = 'bla' AND col = 'blah'

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/136570-solved-and-or-in-syntax/
Share on other sites

That doesn't make any sense... a field can't have multiple values simultaneously!

Hmm, I didn't even realize the query wouldn't even work lol and I was jumping ahead of myself.

To simplify what I have is a product that could have many colors. In one table I will have the name of the product and the product id. In another table I will have the product id as a foreign key and the value could be "blue". If the product has more than one color I will insert another row with the same id and it could have a value of "purple". So how can I find the product id for something that has both blue and purple as color values?

That will work, but it's going to be very bad for index usage (check EXPLAIN) what if 90% of the rows have either, but only 2% have both? Also, depending on what version of mysql, index_merge many not be an option, so using IN() isn't great either.

 

It's better to simply to simply join the table to itself and see if you get any rows back:

 

SELECT t1.id
FROM your_table as t1
CROSS JOIN your_table as t2 USING ( id )
WHERE t1.color = 'blue' and t2.color = 'purple'

It's better to simply to simply join the table to itself and see if you get any rows back:

 

SELECT t1.id
FROM your_table as t1
CROSS JOIN your_table as t2 USING ( id )
WHERE t1.color = 'blue' and t2.color = 'purple'

 

What if I need more colours? More joins?

That will work, but it's going to be very bad for index usage (check EXPLAIN) what if 90% of the rows have either, but only 2% have both? Also, depending on what version of mysql, index_merge many not be an option, so using IN() isn't great either.

 

It's better to simply to simply join the table to itself and see if you get any rows back:

 

SELECT t1.id
FROM your_table as t1
CROSS JOIN your_table as t2 USING ( id )
WHERE t1.color = 'blue' and t2.color = 'purple'

 

Never used something like that, if I need 5 values am I supposed to join the table 5 times? Is this right:

 

SELECT t1.id
FROM your_table as t1
CROSS JOIN your_table as t2
CROSS JOIN your_table as t3
CROSS JOIN your_table as t4
CROSS JOIN your_table as t5 USING ( id )
WHERE t1.color = 'blue' and t2.color = 'purple' and t3.color= 'green' and t4.color= 'white' and t5.color= 'black'

And if even more, I just keep rejoining the table that many times?

I believe like this:

 

SELECT t1.id
FROM your_table as t1
CROSS JOIN your_table as t2 USING ( id )
CROSS JOIN your_table as t3 USING ( id )
CROSS JOIN your_table as t4 USING ( id )
CROSS JOIN your_table as t5 USING ( id )
WHERE t1.color = 'blue' and t2.color = 'purple' and t3.color= 'green' and t4.color= 'white' and t5.color= 'black'

I believe like this:

 

SELECT t1.id
FROM your_table as t1
CROSS JOIN your_table as t2 USING ( id )
CROSS JOIN your_table as t3 USING ( id )
CROSS JOIN your_table as t4 USING ( id )
CROSS JOIN your_table as t5 USING ( id )
WHERE t1.color = 'blue' and t2.color = 'purple' and t3.color= 'green' and t4.color= 'white' and t5.color= 'black'

Yep, that's right. Thanks all.

Oh, one last question. You can't use an INNER JOIN before CROSS JOIN? Say I want to grab other info that's not in those tables and I want to try an INNER JOIN, then CROSS JOIN but it doesn't seem to work:

SELECT t1.id, col
FROM extra as e INNER JOIN your_table as t1
ON e.id = t1.id
CROSS JOIN your_table as t2 USING ( id )
CROSS JOIN your_table as t3 USING ( id )
CROSS JOIN your_table as t4 USING ( id )
CROSS JOIN your_table as t5 USING ( id )
WHERE t1.color = 'blue' and t2.color = 'purple' and t3.color= 'green' and t4.color= 'white' and t5.color= 'black'

I see no reason why that wouldn't work... the only reason I prefer cross join is that I have the conditions in the where clause, which I find more explicit when I'm joining for speed instead of "by design", if you know what I mean.  Besides, what error do you get?

I see no reason why that wouldn't work... the only reason I prefer cross join is that I have the conditions in the where clause, which I find more explicit when I'm joining for speed instead of "by design", if you know what I mean.  Besides, what error do you get?

If I run that statement without the INNER JOIN it works fine. Once I add the inner join it tells me:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.id)
WHERE t1.color = 'blue' AND t2.color = 'purple'

Using the same query as before, except I only am joining the table twice.

SELECT t1.id, col
FROM extra as e INNER JOIN your_table as t1
ON e.id = t1.id
CROSS JOIN your_table as t2 USING (t1.id)
WHERE t1.color = 'blue' AND t2.color = 'purple'

Done what? The whole point of USING() is that you don't need the prefix.

Oh I see...

 

Originally I had done this:

SELECT t1.id, col
FROM extra as e INNER JOIN your_table as t1
ON e.id = t1.id
CROSS JOIN your_table as t2 USING (id)
WHERE t1.color = 'blue' AND t2.color = 'purple'

 

So that's what "using" does hehe. I switched it to USING instead of ON for the inner join and now it works. Thanks again. :)

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.