Is there开发者_Go百科 away to select a table column on a condition ?, in this query :
SELECT x,y FROM Z
I want to make selecting the y column Y is conditioned, as:
SELECT x ( ONLY WHERE x_type = '20' ), y FROM Z # just example of what i am trying to do :)
y_type is another column, it's value is what on which selecting Y or not depends !, Is there away to do that, or i am just hitting the wrong door ?
No, this is not possible. You could set the column to NULL if the condition is not met, though:
SELECT IF (x_type = 20, x, NULL) AS x
y
FROM Z
SELECT CASE WHEN y_type = 20 THEN y ELSE NULL END As y -- replace y_type with column to check
,.... -- other column list
FROM yourtable
You can also use a CASE with IN for multiple values eg.
SELECT CASE WHEN y_type IN (10,20) THEN y ELSE NULL END As y -- replace y_type with column to check
,.... -- other column list
FROM yourtable
select case when x_type = '20' then
x else '' end,y from z
Not sure about the details:
SELECT x
, CASE WHEN x_type = '20' THEN y
ELSE NULL
END AS y
FROM Z
or perhaps you want the simple:
SELECT x
, y
FROM Z
WHERE x_type = '20'
If you want to select on x_type, then your SQL query should be
SELECT x, y FROM Z where x_type = '20'
If you'd like to constrain on y_type, then you might want to add OR y_type = 'nn'
to the end. I think your question is whether or not you need to have the same column in both the select clause and in the where clause. The answer is "no, you don't have to."
精彩评论