phpinfo() Posted November 28, 2008 Share Posted November 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
swizzer Posted November 28, 2008 Share Posted November 28, 2008 I'm having difficulty understanding what kind of query you are trying to build. Please try and clarify with some sample data from different tables, and I'll help with your query. Quote Link to comment Share on other sites More sharing options...
trampolinejoe Posted November 28, 2008 Share Posted November 28, 2008 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 Quote Link to comment Share on other sites More sharing options...
phpinfo() Posted November 28, 2008 Author Share Posted November 28, 2008 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. Quote Link to comment Share on other sites More sharing options...
phpinfo() Posted November 28, 2008 Author Share Posted November 28, 2008 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 Quote Link to comment Share on other sites More sharing options...
trampolinejoe Posted November 29, 2008 Share Posted November 29, 2008 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? Quote Link to comment Share on other sites More sharing options...
trampolinejoe Posted November 29, 2008 Share Posted November 29, 2008 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 } Quote Link to comment Share on other sites More sharing options...
haku Posted November 29, 2008 Share Posted November 29, 2008 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.