Exception in sql query

Asked

Viewed 789 times

1

How do I view my Mysql database items in php, so that you select all items except one item. Ex: I have several records...

item 1   |   id01
item 2   |   id02
item 3   |   id03
item 4   |   id04
item 5   |   id05
item 6   |   id06

I want to display all in php except the item with id04.

my code...

$itemcheck = $dbh->prepare("select * FROM itens ORDER BY it_data DESC");
$itemcheck->execute();
while ( $cm_item = $itemcheck->fetch(\PDO::FETCH_OBJ) ){
}

I’m not sure I made myself clear in my question.

3 answers

3


Can display all records except id 4 com using the operator different/not Equal <> or !=

$itemcheck = $dbh->prepare("SELECT * FROM itens WHERE id <> ? ORDER BY it_data DESC");

$itemcheck->execute(array(4));
while ( $cm_item = $itemcheck->fetch(\PDO::FETCH_OBJ) ){

}
  • Thanks, solved my problem. :)

2

Try to be as clear as possible in your question. Apparently by SQL itself you solve.

$itemcheck = $dbh->prepare("select * FROM itens where id not in (4) ORDER BY it_data DESC");

1

It’s not clear what you want, but come on.

Do you want to select all COLUMNS except one? If this is the case, you should pass the columns you want to use:

select col1,col3,col4,col6 FROM itens ORDER BY it_data DESC

If you want to select all lines, except one:

SELECT * FROM itens WHERE id != "1" ORDER BY it_data DESC
ou
SELECT * FROM itens WHERE id NOT IN (1) ORDER BY it_data DESC -> (como o Marllon postou)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.