SQL:如何根据第三个字段选择两个字段之一
我需要从现有表中导出数据。直到最近我使用 -
SELECT item_ID, item_Info, ...moreFields... FROM tableName WHERE myCondition=0
现在,他们更改了表结构,并添加了新字段“item_Info2”
当他们填写表时:
- 如果“item_Type”是1或2或3或4,那么
- 如果“item_Type”则 “item_Info”是相关的是5,那么“item_Info”为空,我需要“item_Info2”作为我的查询结果,而不是“item_Info”
对应的SELECT命令是什么?
[类似问题:mysql 根据第三个字段的值从两个字段中选择任意一个字段
但是 在此示例中我看不到选择 moreFields 的语法]
谢谢,
Atara 。
I need to export data from an existing TABLE. Till recently I used -
SELECT item_ID, item_Info, ...moreFields... FROM tableName WHERE myCondition=0
Now, they changed the TABLE structure, and added new field "item_Info2"
When they fill in the TABLE:
- if "item_Type" is 1 or 2 or 3 or 4, then "item_Info" is relevant
- if "item_Type" is 5, then "item_Info" is empty, and I need "item_Info2" as my query result, instead of "item_Info"
What is the corresponding SELECT command?
[similiar question: mysql select any one field out of two with respect to the value of a third field
but from this example I cannot see the syntax for selecting moreFields ]
Thanks,
Atara.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将
CASE
语句视为任何其他列名,用逗号等将其与其他列分隔开。CASE
中发生的情况应被视为单个列名,在您的情况下,您需要在其前后添加逗号。您还可以使用别名来更轻松地从查询中获取结果:
You can treat the
CASE
statement as any other column name, separate it from the other columns with a comma, etc. What happens in theCASE
should be considered a single column name, in your case you need commas before and after it.you could also use an alias to be easier to get the result from the query:
编辑其他问题的回复:
To edit the reply on the other question: