0

I have a question regarding an SQL syntax.

I have a mysql table like this (id, name, subid, preview) where preview is set to 0 as default. So now I want to make a select to query only lines where preview is different from zero.

Can I do this in a single query .. or I need to query all and then make (if else) decisions?

like SELECT * FROM table_name; - and iterate through ... ?

5 Answers 5

1

You could do

select *
from mytable
where preview != 0 

!= means "not equal to". Some databases also use <> for the same meaning.

If preview is nullable, do you also want to return rows where preview is null? If so, you may want to try:

select *
from mytable
where preview != 0 or preview is null
Sign up to request clarification or add additional context in comments.

Comments

1

Put condition in WHERE

SELECT * FROM table_name
WHERE  preview !=0

Comments

1

Also worth to point out is that while * will do fine, you should always try to specify which columns you need in your query, so it be more clear what the query actually retrieves, so

SELECT id, name, subid, preview from table_name WHERE preview != 0

is more clear than SELECT *, but anyway all the other answers are right too.

Comments

0

What about

SELECT * FROM table_name WHERE preview != 0;

?

Comments

0

You can simply use query like this: select * from table_name where preview <> 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.