我試過了,但沒有運氣,我有兩個表「crm_rentals」和「crm_sales」。聯盟與多個表在MYSQL問題
兩者具有相同的結構。
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
問題:我可以沒有得到這個結果,我的查詢給我語法錯誤。
UPDATE我試過,但語法錯誤
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 [email protected])
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 [email protected])
as JustRentals) ff) AS f
你忘了問的問題......你在哪裏卡住了嘗試? –
我的問題是,我無法得到這個結果,我的查詢給我問題。 –
我還沒有時間寫一個完整的答案,但它可能值得您一段時間看看這篇文章的想法:[動態數據透視表與MySql](http://stackoverflow.com/questions/12630128/mysql-動態樞軸)。在你的情況下,數據透視查詢mabe不需要是動態的,所以只需直接使用post中顯示的生成的select查詢。你也應該能夠在你的桌子上做一個'UNION ALL',然後從那裏開始工作。 – cars10m