Issue combining two SQL queries

余生颓废 提交于 2020-01-17 05:18:25

问题


Hi everybody I'm trying to combine two MySQL queries into only one.

I have two blocks, the FROM and the WHERE, I think they are the only ones needed in my case.

FROM:

FROM table1, table2, table3, table4, table5

WHERE:

WHERE field1 = 1
  AND (field2 = 3 OR field2 = 4)
  AND (table5.field3 <= 100000)
  AND (table5.field4 >= 1)
  AND (table5.field5 >= 1)
  AND (table5.field6 >= 60)
  AND table5.field2 = table4.id
  AND table5.field7 = table3.field8
  AND table3.field9 = table1.field10
  AND table3.field11 = table2.field12

And the other query is this:

select t.txt, count(t.age) from 
(select 
  case 
      when age<11 then concat(age ,' year') 
      else 'more than 10' 
  end txt, age
from your_table)t
group by t.txt
order by 1

Thanks to @Farhęg

I want that your_table is the result table of the query 1, or with restrictions FROM and WHERE that I already have.

How can I combine the two queries? Thanks!

来源:https://stackoverflow.com/questions/29337700/issue-combining-two-sql-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!