使用单个查询连接三个表
Join three tables using single query
我有三张桌子,我想加入它们。
连接两个表完美,但连接三个表 returns 仅一行
Table 姓名
- 用户详细信息
- 销售轨道
- 付款详情
查询 #1:
SELECT
`userdetails`.CustomerIndex,
`userdetails`.FristName,
SUM(`paymentdetails`.Amountofpaying) TotalPaid
FROM
`userdetails`
LEFT JOIN
`paymentdetails` ON `userdetails`.CustomerIndex=`paymentdetails` .CustomerID
GROUP BY
`userdetails`.CustomerIndex,`userdetails`.FristName
查询#2:
SELECT
`userdetails`.CustomerIndex,
`userdetails`.FristName,
SUM(`saletrack`.Total) Totalbilled
FROM
`userdetails`
LEFT JOIN
`saletrack` ON `userdetails`.CustomerIndex = `saletrack`.CustomerId
GROUP BY
`userdetails`.CustomerIndex, `userdetails`.FristName
查询 #3:
SELECT
s.CustomerId,
SUM(s.Total) Totalbilled,
p.CustomerID,
SUM(p.Amountofpaying) TotalPaid
FROM
`userdetails` AS ud
LEFT JOIN
`saletrack` AS s ON ud.CustomerIndex = s.CustomerId
LEFT JOIN
`paymentdetails` AS p ON ud.CustomerIndex = p.CustomerID
WHERE
p.CustomerID = ud.CustomerIndex
AND s.CustomerId = ud.CustomerIndex
LIMIT 0 , 30
正如您在连接中提到的 ud.CustomerIndex = s.CustomerId && ud.CustomerIndex = p.CustomerID 那么无需输入又是哪里条件。试试这个:
SELECT s.CustomerId, SUM( s.Total ) Totalbilled, p.CustomerID, SUM( p.Amountofpaying ) TotalPaid
FROM `userdetails` AS ud
LEFT JOIN `saletrack` AS s ON ud.CustomerIndex = s.CustomerId
LEFT JOIN `paymentdetails` AS p ON ud.CustomerIndex = p.CustomerID
LIMIT 0 , 30
我们需要添加按查询分组
SELECT s.CustomerId, SUM( s.Total ) Totalbilled, p.CustomerID, SUM( p.Amountofpaying ) TotalPaid
FROM `userdetails` AS ud
LEFT JOIN `saletrack` AS s ON ud.CustomerIndex = s.CustomerId
LEFT JOIN `paymentdetails` AS p ON ud.CustomerIndex = p.CustomerID
WHERE p.CustomerID = ud.CustomerIndex
AND s.CustomerId = ud.CustomerIndex
***GROUP BY ud.CustomerIndex, ud.FristName***
LIMIT 0 , 30
我有三张桌子,我想加入它们。
连接两个表完美,但连接三个表 returns 仅一行
Table 姓名
- 用户详细信息
- 销售轨道
- 付款详情
查询 #1:
SELECT
`userdetails`.CustomerIndex,
`userdetails`.FristName,
SUM(`paymentdetails`.Amountofpaying) TotalPaid
FROM
`userdetails`
LEFT JOIN
`paymentdetails` ON `userdetails`.CustomerIndex=`paymentdetails` .CustomerID
GROUP BY
`userdetails`.CustomerIndex,`userdetails`.FristName
查询#2:
SELECT
`userdetails`.CustomerIndex,
`userdetails`.FristName,
SUM(`saletrack`.Total) Totalbilled
FROM
`userdetails`
LEFT JOIN
`saletrack` ON `userdetails`.CustomerIndex = `saletrack`.CustomerId
GROUP BY
`userdetails`.CustomerIndex, `userdetails`.FristName
查询 #3:
SELECT
s.CustomerId,
SUM(s.Total) Totalbilled,
p.CustomerID,
SUM(p.Amountofpaying) TotalPaid
FROM
`userdetails` AS ud
LEFT JOIN
`saletrack` AS s ON ud.CustomerIndex = s.CustomerId
LEFT JOIN
`paymentdetails` AS p ON ud.CustomerIndex = p.CustomerID
WHERE
p.CustomerID = ud.CustomerIndex
AND s.CustomerId = ud.CustomerIndex
LIMIT 0 , 30
正如您在连接中提到的 ud.CustomerIndex = s.CustomerId && ud.CustomerIndex = p.CustomerID 那么无需输入又是哪里条件。试试这个:
SELECT s.CustomerId, SUM( s.Total ) Totalbilled, p.CustomerID, SUM( p.Amountofpaying ) TotalPaid
FROM `userdetails` AS ud
LEFT JOIN `saletrack` AS s ON ud.CustomerIndex = s.CustomerId
LEFT JOIN `paymentdetails` AS p ON ud.CustomerIndex = p.CustomerID
LIMIT 0 , 30
我们需要添加按查询分组
SELECT s.CustomerId, SUM( s.Total ) Totalbilled, p.CustomerID, SUM( p.Amountofpaying ) TotalPaid
FROM `userdetails` AS ud
LEFT JOIN `saletrack` AS s ON ud.CustomerIndex = s.CustomerId
LEFT JOIN `paymentdetails` AS p ON ud.CustomerIndex = p.CustomerID
WHERE p.CustomerID = ud.CustomerIndex
AND s.CustomerId = ud.CustomerIndex
***GROUP BY ud.CustomerIndex, ud.FristName***
LIMIT 0 , 30