Jump to content

WHERE Query Help


Canman2005

Recommended Posts

Hi all

 

I have the following database

 

itemid    primary

1          1

1          0

2          1

3          0

3          0

4          0

 

The field `itemid`refers to the item ID in another database, the `primary` field refers to which rows are the primary rows, basically any rows that have the same `itemid` means they belong together.

 

How can I do a QUERY to grab all rows where there is no `primary` set to 1, so with my above data, you would get the following

 

itemid    primary

3          0

3          0

4          0

 

it only returns this is because `itemid` 2 has its `primary` set to 1 and also `itemid` 1 has at least one of its rows `primary` set to 1

 

Does this make sense? Can anyone help

 

Thanks

 

Dave

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

Sorry might not have explained it correct.

 

I guess

 

$sql = "SELECT * FROM `items` WHERE `primary`!='1' ";

 

would work.

 

But if there are two rows for example with the same itemid and one of those rows has `primary` set to 1, then it would not return either row. But then if there were two rows with the same `itemid` and neither of them have `primary` set to 1, then it would return them

 

Does that make much sense?

Link to comment
https://forums.phpfreaks.com/topic/136819-where-query-help/#findComment-714579
Share on other sites

If I understand correctly, you should use a subquery

 

select * from items where not exists (select * from item where primary = 1)

 

In this way, if the itemid has ANY record where primary is set to one, it will not return that itemid

 

Is this what you are looking for?

Link to comment
https://forums.phpfreaks.com/topic/136819-where-query-help/#findComment-714580
Share on other sites

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.