2015-10-18 48 views
0

我試過了,但沒有運氣,我有兩個表「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 
+0

你忘了問的問題......你在哪裏卡住了嘗試? –

+0

我的問題是,我無法得到這個結果,我的查詢給我問題。 –

+0

我還沒有時間寫一個完整的答案,但它可能值得您一段時間看看這篇文章的想法:[動態數據透視表與MySql](http://stackoverflow.com/questions/12630128/mysql-動態樞軸)。在你的情況下,數據透視查詢mabe不需要是動態的,所以只需直接使用post中顯示的生成的select查詢。你也應該能夠在你的桌子上做一個'UNION ALL',然後從那裏開始工作。 – cars10m

回答

0
select 
    sum(a.dubizzle) as dubizzle, 
    sum(b.JustRentals) as JustRentals, 
    sum(c.JustProperty) as JustProperty, 
    sum(d.propertyfinder) as 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%' 
    ) as 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%' 
    ) as 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%' 
    ) as 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%' 
    ) as d 
) 

請試試這個,在這裏返回查詢結果。

BTW儘量保持你的代碼清楚:)

+0

您的語法中有錯誤 –

+0

@MuhammadAli在哪一行? – Liniel

+0

服務器版本正確的語法使用近「)一個,在第16行 –

1

您可以通過這種方式

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 
)