开发者

MYSQL query if syntax

I 开发者_如何学JAVAhave 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 ... ?


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


Put condition in WHERE

SELECT * FROM table_name
WHERE  preview !=0


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.


What about

SELECT * FROM table_name WHERE preview != 0;

?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜