Jump to content

db select value where value = No, or nothing at all?


phpinfo()

Recommended Posts

I have some query statements to show certain data based items selected from a DB.

 

SELECT value FROM table WHERE value = "no"

 

The problem is, I don't want to have to do a db insert for each id on a new table. - How would I write the SELECT code, so I can join it with other statements, where value = a userid that has No in this new table, but show all other users, even if they have yet to be entered into the table?

 

Basically select all No's - and show all other ids whether they are in the new table or not.

Link to comment
Share on other sites

Here is a example of joining tables, that I actually did yesterday and it worked pretty well, show us more info about what you want to do so we can help adjust it for you.

 

SELECT O.customernumber, Lay.id, Lay.row, Lay.date, Lay.payment, Lay.credit, Lay.balance
FROM orders O, layby Lay
WHERE O.customernumber = '$id'
AND  O.connote = Lay.id

Link to comment
Share on other sites

Basically I want to select certain tables, and show information based on the results.

 

SELECT a.*, b.special from accounts a left outer join special_tb b on on etc. etc.

 

WHERE a.id = $id and b.special!='Yes'

 

So basically I want to show all results, but if the new special table has a row for a certain id that says Yes, that is excluded.

 

--- The problem is, if the rest of the rows in the special table don't say No for every id, the results won't show up.

 

So is there a way to say : (If b.special says Yes, exclude that from the results, but if it says NO or there is no row at all for that ID, then display the result for that ID.

 

I don't want to have to enter thousands of "NOs" in the new table. Just exclude if there is a Yes.

Link to comment
Share on other sites

Simpler explanation.

 

I am a user, viewing all results from a table that has an ID and content fields.

 

I want to view the content for every id - but if in another table, an ID has exclude = Yes, don't show that one.

 

$query = "select

 

a.id,a.content,

 

b.no_id, b.exclude

   

from accounts a

   

left outer join excludetable b on b.no_id = a.id

   

where ((b.exclude!='Yes') order by rand() limit 10";

 

 

But if the exclude table, does not have an entry for the ID, those IDs will not show in the results. - How would you code it to show all rows from accounts, unless table exlcude b.exclude=Yes for a certain Id, then don't show that ID

Link to comment
Share on other sites

I do not know if this is the best way to do it.

 

But I am guessing if it was be I would just do it in the loop that is building the table?

 

(presuming we make a query which selects a.id, a.content, b.no_id, b.exclude)

        while($row = mysql_fetch_array($query))
        {
            if($exclude!="yes"){
            echo "<td>$row[0]<td>"; // print id
            echo "<td>$row[1]<td>";//  print content
            echo "<td>$row[2]<td>";// print num id
            }
         $exclude =$row[3]<td>"; // set exclude
        }

 

You could use a for loop, same thing. If you had heaps of rows you would use a for loop.

Is this what you mean?

Link to comment
Share on other sites

i mean:

   while($row = mysql_fetch_array($query))
        {
            if($exclude!="yes"){
            echo "<td>$row[0]<td>"; // print id
            echo "<td>$row[1]<td>";//  print content
            echo "<td>$row[2]<td>";// print num id
            }
         $exclude =$row[3]; // set exclude
        }

Link to comment
Share on other sites

Not the best way (I'm not good with joins), but a way:

 

SELECT * FROM table1 WHERE id IN (SELECT id FROM table2 WHERE exclude != 'yes')

 

Or something like that. I would have to play around with it to make sure it's right, but maybe it can point you in the right direction.

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.