在MySql中,如何仅根据我选择的特定列的值选择特定列?
"id" "postTitle" "postBody" "postCategory" "postStatus" "userId" "userName"
"1" "Title 1" "Body 1" "Alcohol" "0" "1" "norman"
"2" "Title 2" "Body 2" "Books" "1" "1" "norman"
例如:从上表中,我尝试仅选择
postTitle
(如果postStatus = 0
),但是选择postTitle
&postBody
&postCategory
,如果postStatus != 0
它可以解决类似这样的问题,但这是行不通的
select if postStatus != 0 then
postTitle, postBody, postCategory
else
postTitle
End If
from tablea where id = 2;
我尝试使用大小写,但它只适用于一列,如果我选择更多则不行
select case postStatus when 1 then
postTitle, postBody
else
postTitle
End
from tablea where id = 2;
有没有办法像我正在尝试做的那样?
最佳答案
尝试使用UNION运算符,
SELECT postTitle ,'' AS postBody ,'' AS postCategory FROM tableName
WHERE postStatus = '0'
UNION
SELECT postTitle ,postBody , postCategory FROM tableName
WHERE postStatus != '0'