Is it possible to use a 'select * where...' with two conditions?

I am stumped. I need to use a select/where clause but I need to check for one condition to be true and a second one to be false on the same column. I have no problem getting either of the two to work but when I put them together I get no results. I have tried putting one in front and then reversing them and tried every syntax I can guess at. And I can’t find this exact issue anywhere on here, StackOverflow or on the web.

Is it illegal to try and put two conditions after the WHERE or is it just wrong to use a LIKE and then a NOT LIKE?

sure, though i woudl say it’s rather rare to want a positive and negative test against the same column, it’s certainly doable by putting an AND between the two conditions.

It depends on the conditions. Something like

Column = 1 AND Column <> 1

obviously will never return any results.

Why don’t you tell us the conditions needed and your code so far?

Gentlemen, thank you both for your help. It turns out it was just the loose nut behind the keyboard. As I was trying to recreate all of the possible syntax I had tried I double checked the one I thought should work. It turns out that I had misspelled the field I was comparing. The field was ‘toppos’ and I had typed in topos’ (single p). When I corrected that it worked fine. I had it right, I just can’t spell! [Grin]

In answer to why I would want to do that… I have this database to follow 9 Billboard charts every week. Each one has a letter designation for what chart that entry is following. Unfortunately I ended up with two that both started with A. The original A was for the Adult Contemporary charts. The one I added later Adult Pop got the AP designation. If I was just searching for the latter (AP) I had no problem but when I searched for ‘A%’ I got both. That’s why I needed one positive and the other negative.

Final working query is:

$result = mysqli_query($conn, "SELECT * from data where toppos like 'A%' and toppos not like 'AP%' order by artist, title, toppos");

Again, Thanks.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.