Jump to content


Photo

Query Select All Except 1


  • Please log in to reply
3 replies to this topic

#1 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 29 June 2006 - 11:42 AM

Hey it's me again. [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]
Anyone know how to do a sql query which select all except one, if I'm not wrong it has some like "NOT".
But I couldn't get it right, think I didn't get the query working.

$query = "SELECT * FROM table";
$result = mysql_query($query) or die("Query failed: " . mysql_error());
while($row = mysql_fetch_assoc($result)) {
}

Thanks again [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

Edit: Ops, sorry to tell, I doesn't want it to echo out id 1.
Table structure: id, date and content.

Edit2: Ops lolz, just a few second after I post. An idea just flash through my mind.
$query = "SELECT * FROM table";
$result = mysql_query($query) or die("Query failed: " . mysql_error());
while($row = mysql_fetch_assoc($result)) {
if ($row['id'] == 1) {
} else {
}
}

But I know it's not the best way, so anyone know how to do in other way ?

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.


#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 29 June 2006 - 12:00 PM

ok, so you do not want to include id 1 into your result:
simple.

$query = "SELECT * FROM table where id != 1";
Life's too short for arguing.

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 29 June 2006 - 12:27 PM

...or, if you end up having more than one restriction you want to filter out, just use NOT IN:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * FROM tableName WHERE id NOT IN ('1', '2', '3');
[!--sql2--][/div][!--sql3--]
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 hackerkts

hackerkts
  • Members
  • PipPipPip
  • Advanced Member
  • 593 posts
  • LocationSingapore
  • Age:18

Posted 29 June 2006 - 03:13 PM

Thanks hvle & obsidian, oh.. So I forgotten the "IN" :s
Thanks again :D

*staring at the title "Super Guru" :P*

Regards,
hackerkts

To be a coder, you must learn how to think and not to give up so easily.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users