Mysql 在两种不同的条件下从同一表中的同一字段查询多个列

Mysql 在两种不同的条件下从同一表中的同一字段查询多个列,mysql,sql,Mysql,Sql,此tblName表有4列{Date}、{Status}、{mean}、{Type} 我想使用side子查询中的条件在不同的列中显示 Select Date, Status, Meal (Select Type as Special from tblName where Type In ('SSS','MMM')), (Select Type as Normal from tblName where Type Not IN ('SSS','M

此tblName表有4列{Date}、{Status}、{mean}、{Type} 我想使用side子查询中的条件在不同的列中显示

Select Date, Status, Meal
    (Select Type as Special
     from tblName
     where Type In ('SSS','MMM')),
    (Select Type as Normal
     from tblName
     where Type Not IN ('SSS','MMM'))
From tblName
我收到了错误信息

Msg 512, Level 16, State 1, Line 1
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.
Msg 512,第16级,状态1,第1行
子查询返回了多个值。当子查询在=、!=、=或者当子查询用作表达式时。

可能是这样的:

Select t1.Date, t1.Status, t1.Meal
    (t2.Type as Special),
    (t3.Type as Normal )
From tblName t1 LEFT JOIN
tblName t2
ON t1.ID=t2.ID
LEFT JOIN 
tblName t3
ON t1.ID=t3.ID
where t2.Type Not In ('SSS','MMM') 
OR t3.Type In ('SSS','MMM') 

条件聚合

Select Date, Status, Meal
    (case when Type IN ('SSS','MMM') then Type else null end case) 
     )Type as Special,
   (case when Type NOT IN ('SSS','MMM') then Type else null end case) 
     )Type as Normal
From tblName

您要做的是:对于tblName中的每个记录,从类型所在的tblName中选择所有类型('SSS','MMM')。所有这些类型应在tblName的结果行中显示为一列。那当然不行。您可以为tblName中的每条记录选择一个值。例如
max(type)

然而,当类型为“SSS”或“MMM”时,您实际上想要的只是显示“特殊”,否则显示“正常”

Select Date, Status, Meal,
  case when Type In ('SSS','MMM') then 'special' als 'normal' end as kind
From tblName;
或者在两个单独的列中显示类型

Select Date, Status, Meal,
  case when Type In ('SSS','MMM') then Type end as Special,
  case when Type Not In ('SSS','MMM') then Type end as Normal
From tblName;

您能为我们提供一个SQLFIDLE示例和期望的结果吗?解决您的问题:运行
从tblName where Type In('SSS','MMM')中选择Type as Special,
从tblName where Type Not In('SSS','MMM')中选择Type as Normal
在您的表格上,看到有一个返回的结果列表,当在
选择列表中使用时,您的位置不允许使用该列表。我将尽力解释,很抱歉,这有点复杂,因为我是新手。我想要的结果是,在新名称中有一个新的表分隔列“Type”,在不同的结果中是特殊的和正常的。有什么方法可以做到这一点吗?在'Type'中,column有许多不同的值,我想创建一个新列,其中只有'SSS'和'MMM'值以及其他不在'SSS'和'MMM'中的值“MMM”将存储在“正常”列中,如果在许多行中
类型不同,则
日期
状态
膳食
相等?请考虑一个最小的SqLFIDLE示例,它为我们节省了很多评论在这里!