mysql - MYSQL 中多个表的 UNION 问题

标签 mysql union

我尝试过,但没有运气,我有两个表“crm_rentals”和“crm_sales”。

两者具有相同的结构。

Id | portals_name
1  | {dubizzle}{JustRentals}{JustProperty}{propertyfinder}{bayut}
2  | {dubizzle}{JustRentals}{JustProperty}{propertyfinder}{bayut}
3  | {JustRentals}{JustProperty}{propertyfinder}
4  | {dubizzle}{JustProperty}{bayut} 

我想获取两个表中每个门户的编号,这是我尝试过的

select sum(dubizzle) dubizzle,sum(JustRentals) JustRentals,
sum(JustProperty) JustProperty,sum(propertyfinder) propertyfinder 
from ( (select count(id) as dubizzle from crm_rentals where 
portals_name like '%dubizzle%'
UNION
select count(id) as dubizzle from crm_sales where portals_name 
like  '%dubizzle%'
) a ,
(select count(id) as JustRentals from crm_rentals where 
 portals_name like '%JustRentals%'
UNION
select count(id) as JustRentals from crm_sales where 
portals_name like  '%JustRentals%') b,
(select count(id)  as JustProperty from crm_rentals where 
portals_name like '%JustProperty%'
UNION
select count(id)  as JustProperty from crm_sales where portals_name 
like '%JustProperty%') c ,
(select count(id) as propertyfinder from crm_rentals where 
portals_name like '%propertyfinder%'
UNION
select count(id) as propertyfinder from crm_rentals where 
portals_name like '%propertyfinder%'
) d )

我想要得到像这样的结果

Dubizzle    JustRentals  JustProperty Propertyfinder Others
100           100         100          100            100

问题:我无法得到此结果,我的查询给出语法错误。

更新我尝试了这个,但语法错误

select * from (select @table1:=(select count(id) as dubizzle 
from  crm_rentals where portals_name like '%dubizzle%') a,
@table2:=(select count(id) as dubizzle from crm_sales 
where portals_name   like '%dubizzle%') b, (@table1 +@table2) 
as dubizzle) f,
((select @table1:=(select count(id) as JustRentals from 
crm_rentals where portals_name like '%JustRentals%') c,
@table2:=(select count(id) as JustRentals from crm_sales 
where portals_name like '%JustRentals%') d, (@table1 +@table2)
 as JustRentals)  ff) AS f

最佳答案

你可以试试这个方法

SELECT * FROM ( 
(select count(id) as dubizzle from crm_rentals where portals_name like '%dubizzle%') AS a,
(select count(id) as JustRentals from crm_rentals where portals_name like '%JustRentals%') b,
(select count(id)  as JustProperty from crm_rentals where portals_name like '%JustProperty%') AS c
UNION
(select count(id) as dubizzle from crm_sales where portals_name like '%dubizzle%') AS a,
(select count(id) as JustRentals from crm_sales where portals_name like  '%JustRentals%') AS b,
(select count(id)  as JustProperty from crm_sales where portals_name like '%JustProperty%') AS c
)   

关于mysql - MYSQL 中多个表的 UNION 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33195619/

相关文章:

MySQL 创建外键时出现错误 1215

从数据库导出时 PHPexcel 库错误

使用带联合的派生表的 MySQL 列排序问题

sql - 别名派生表是两个选择的并集

dataframe - Julia DataFrame 行数据类型已更改?

mysql选择具有最低值的记录

mysql - 是否有可能在任何项目中更改 MySQL 数据库表的字段名称或在 Joomla 中添加新字段?

MySQL 错误 1822 : Failed to add foreign key constraint; missing index for contraint BUT index exists

用于在一个查询中获取付款和发票的 MySQL 查询

sql - 在 postgresql 中获取空/打开时间范围