选择左(columnA,2)或右(columnA,1)
MySql 有没有办法在选择列中使用“或”。例如,
select left(columnA,2) or right(columnA,1) as columnAlias, sum(columnB)
from table
where ((left(columnA,2) in ('aa','bb','cc')) or (right(columnA,1) in ('a,','b','c')))
group by columnAlias
我有一个表,其中列的前 2 个字符或列的最后一个字符表示设施。我需要按设施对值进行求和。联合让我部分地到达那里,然后我可以循环遍历结果数据集并在代码中求和(或者执行存储过程来返回总和),但我想知道是否有一种方法可以从查询中获取它。
我尝试使用联合查询作为动态临时表并对其进行选择和分组,但是如果没有从任何一个选择语句返回记录,那么它会抛出“列columnA不能为空”错误。
也尝试过使用上面的语法,但没有得到我期望的结果还有其他方法可以通过查询来做到这一点吗?
Is there a way in MySql to use an either or in a select column. For instance
select left(columnA,2) or right(columnA,1) as columnAlias, sum(columnB)
from table
where ((left(columnA,2) in ('aa','bb','cc')) or (right(columnA,1) in ('a,','b','c')))
group by columnAlias
what I have is a table where either the first 2 characters of the column or the last character of the column indicates the facility. I need to sum the values by facility. A union gets me part way there then I could loop through the resulting dataset and sum things up in the code (or do a stored proc to return the sums), but I am wondering if there is a way to just get it from the query.
I've tried using the union query as an on the fly temp table and doing the select and group on that but if there are no records returned from either of the select statments then it throws a "column columnA cannot be null error.
Also tried with the syntax above, but not getting the results I am expecting. Any other ways to do this through the query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 CASE 可能是你最好的选择。
http://dev.mysql.com/doc/refman/5.0 /en/case-statement.html
using a CASE would prob be your best bet here.
http://dev.mysql.com/doc/refman/5.0/en/case-statement.html