Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL中具有多个连接的复杂查询_Mysql - Fatal编程技术网

MySQL中具有多个连接的复杂查询

MySQL中具有多个连接的复杂查询,mysql,Mysql,我一直在尝试(未成功)编写一个包含多个联接的查询,这就是我现在请求您帮助的原因 表格结构: SELECT p.name, SUM(IFNULL(carparent.price, 0)+IFNULL(carchild.price, 0)) total FROM parent p # first get the childrens cars INNER JOIN child c ON c.parent_id = p.id INNER JOIN car carchild ON carchild.c

我一直在尝试(未成功)编写一个包含多个联接的查询,这就是我现在请求您帮助的原因

表格结构:

SELECT p.name,
SUM(IFNULL(carparent.price, 0)+IFNULL(carchild.price, 0)) total
FROM parent p 

# first get the childrens cars
INNER JOIN child c ON c.parent_id = p.id
INNER JOIN car carchild ON carchild.child_id = c.id

# now get the parents cars
LEFT JOIN car carparent ON carparent.parent_id = p.id

GROUP BY p.name
ORDER BY total DESC
  • 有很多孩子和很多车的父母
  • 有一个父母和许多汽车的孩子
  • 一辆有一个父母或一个孩子的汽车

(来源:)

我想做什么:

SELECT p.name,
SUM(IFNULL(carparent.price, 0)+IFNULL(carchild.price, 0)) total
FROM parent p 

# first get the childrens cars
INNER JOIN child c ON c.parent_id = p.id
INNER JOIN car carchild ON carchild.child_id = c.id

# now get the parents cars
LEFT JOIN car carparent ON carparent.parent_id = p.id

GROUP BY p.name
ORDER BY total DESC
我试图找出每位家长的汽车总价值。(父母“拥有”子女的汽车,因此子女汽车的价值总和应加在父母汽车的价值总和上)

例如,我希望我的查询返回:

  • 如果托马斯有一辆价值5000英镑的汽车,而有两辆汽车的一个孩子也值5000英镑,那么托马斯的总价值为5000+5000*2=15000英镑

  • 如果John有两辆价值2000英镑的汽车,两个孩子每人有两辆价值2000英镑的汽车,则John的总价值为:2000*2+2000*2+2000*2=12000

简言之:

name   | total
--------------
Thomas | 15000
John   | 12000
到目前为止,我有:

SELECT p.name,
SUM(IFNULL(carparent.price, 0)+IFNULL(carchild.price, 0)) total
FROM parent p 

# first get the childrens cars
INNER JOIN child c ON c.parent_id = p.id
INNER JOIN car carchild ON carchild.child_id = c.id

# now get the parents cars
LEFT JOIN car carparent ON carparent.parent_id = p.id

GROUP BY p.name
ORDER BY total DESC
当父母有多个孩子时,会返回奇怪的结果。。。我想我对所有的连接都感到困惑了

无论如何,如果有人能给我指出正确的方向,我将非常感激:)


非常感谢,

我试图按照前面所述创建您的数据库

这是我的解决办法

select 
Name,
(select sum(price) 
    from car 
    where car.Parent_id=parent.id) as `Value Parent Car(s)`, 
(select sum(price) 
    from car 
    left join child 
    on car.child_id=child.id 
    where child.parent_id=parent.id) as `Value of Child car(s)`
from parent
或者获得一个总数

select 
id, 
Name,
((select sum(price) 
    from car 
    where car.Parent_id=parent.id) +
(select sum(price) 
    from car 
    left join child 
    on car.child_id=child.id 
    where child.parent_id=parent.id)) as `Total Value car(s)`
from parent

我相信,虽然可以找到一个更好的结构。。。举例来说,一辆车是一个人所有的,一个人可以是父母、孩子或两者都是……

你能摆脱父母表吗?一个汽车记录总是填写父母id吗?不,如果汽车是孩子所有的,父母id为空,反之亦然,如果汽车是父母所有的,child_id为null。我会选择一个完全不同的表结构。您可以更改模式,还是必须坚持?是的,我可以更改表结构,您对此有何想法?