Exception in sql query

1

How do I display items from my MySql database in php, so that I select all items except one item. Ex: I have several registrations ...

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 do not know if I was clear on my question.

    
asked by anonymous 29.06.2015 / 17:10

3 answers

3

You can display all records except for the id 4 with the 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) ){

}
    
29.06.2015 / 17:17
2

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

$itemcheck = $dbh->prepare("select * FROM itens where id not in (4) ORDER BY it_data DESC");
    
29.06.2015 / 17:14
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 but one line:

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)
    
29.06.2015 / 17:42