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