$whls = querywheels("SELECT * FROM (pc.pn_partcar AS partnum, pc.name_partcar AS descript, pc.weight_partcar AS weight, pc.cycletime_partcar AS cycletime, pc.cavity_partcar AS cavity, p.name_proses AS proses, mm.material_name AS material FROM partcar as pc
INNER JOIN proses AS p ON p.id_proses = pc.id_prosesfk
INNER JOIN material AS mm ON mm.material_id = p.material_idfk
INNER JOIN detailassembly AS da ON da.partcar_idfk = pc.id.partcar
UNION
b.pn_barbell AS partnum, b.type_barbell AS descript, wh.cycletime_wheel AS cycletime, wh.cavity_wheel as cavity FROM barbell AS b
INNER JOIN wheel AS wh ON b.id_wheelfk = wh.id_wheel
INNER JOIN detailassembly AS da ON barbell_idfk = b.barbell_id)
WHERE pc.id_carfk = c.id_car FROM car AS c AND b.id_carfk = c.id_car");
这是我的代码,我不知道我的错在哪里,有人可以帮我纠正这段代码吗?我很困惑。
这是我的数据库
我想制作像这样的输出
|ID Car|partnum|descript |cycletime|cavity|proses|material|Qty |
|---------------------------------------------------------------|
|N4002 |22222 |partcar1 | 23 | 3 | PUM | Plactic| 1 |
| |22222 |partcar2 | 23 | 3 | PUM | Plactic| 1 |
| |22222 |partcar3 | 23 | 3 | PUM | Plactic| 1 |
| |22222 |partcar4 | 23 | 3 | PUM | Plactic| 1 |
| |22233 |Barbell1 | 20 | 3 | PUM | Plactic| 2 |
请帮我修改我的代码。我无法运行它。
编辑: 这是我的错误 警告:mysqli_fetch_assoc()期望参数1为mysqli_result,第17行的C:\ xampp \ htdocs \ hwbase \ PHP \ connect.php中给出布尔值
这是我的功能
function querywheels($sql)
{
global $con;
//query for takes data
$result = mysqli_query($con,$sql);
/*$whls = mysqli_fetch_assoc($result);*/
$rows = [];
while ($whs = mysqli_fetch_assoc($result)) {
$rows []= $whs;
}
return $rows;
}
答案 0 :(得分:0)
尝试此查询
SELECT *
FROM
(
SELECT
pc.pn_partcar AS partnum,
pc.name_partcar AS descript,
pc.weight_partcar AS weight,
pc.cycletime_partcar AS cycletime,
pc.cavity_partcar AS cavity,
p.name_proses AS proses,
mm.material_name AS material,
pc.id_carfk,
da.qty
FROM partcar as pc
JOIN proses AS p ON p.id_proses = pc.id_prosesfk
JOIN material AS mm ON mm.material_id = p.material_idfk
JOIN detailassembly AS da ON da.partcar_idfk = pc.id_partcar
UNION ALL
SELECT
b.pn_barbell AS partnum,
b.type_barbell AS descript,
NULL, -- weight
wh.cycletime_wheel AS cycletime,
wh.cavity_wheel as cavity,
NULL, -- proses
NULL, -- material
b.id_carfk,
da.qty
FROM barbell AS b
JOIN wheel AS wh ON b.id_wheelfk = wh.id_wheel
JOIN detailassembly AS da ON da.barbell_idfk = b.id_barbell
) q
JOIN car AS c ON q.id_carfk = c.id_car
如果您不需要car
中的任何信息(例如pn_car
,description
),则只能使用子查询
SELECT
pc.pn_partcar AS partnum,
pc.name_partcar AS descript,
pc.weight_partcar AS weight,
pc.cycletime_partcar AS cycletime,
pc.cavity_partcar AS cavity,
p.name_proses AS proses,
mm.material_name AS material,
pc.id_carfk,
da.qty
FROM partcar as pc
JOIN proses AS p ON p.id_proses = pc.id_prosesfk
JOIN material AS mm ON mm.material_id = p.material_idfk
JOIN detailassembly AS da ON da.partcar_idfk = pc.id_partcar
UNION ALL
SELECT
b.pn_barbell AS partnum,
b.type_barbell AS descript,
NULL, -- weight
wh.cycletime_wheel AS cycletime,
wh.cavity_wheel as cavity,
NULL, -- proses
NULL, -- material
b.id_carfk,
da.qty
FROM barbell AS b
JOIN wheel AS wh ON b.id_wheelfk = wh.id_wheel
JOIN detailassembly AS da ON da.barbell_idfk = b.id_barbell
答案 1 :(得分:0)
您必须更正查询中的许多内容:
这是一个格式良好的查询
SELECT c.id_car
, t.*
FROM ( SELECT id_carfk
, pc.pn_partcar AS partnum
, pc.name_partcar AS descript
, pc.weight_partcar AS weight
, pc.cycletime_partcar AS cycletime
, pc.cavity_partcar AS cavity
, p.name_proses AS proses
, mm.material_name AS material
FROM partcar AS pc
INNER JOIN proses AS p
ON p.id_proses = pc.id_prosesfk
INNER JOIN material AS mm
ON mm.material_id = p.material_idfk
INNER JOIN detailassembly AS da
ON da.partcar_idfk = pc.id.partcar
UNION
SELECT id_carfk
, b.pn_barbell AS partnum
, b.type_barbell AS descript
, NULL
, wh.cycletime_wheel AS cycletime
, wh.cavity_wheel AS cavity
, NULL
, NULL
, NULL
FROM barbell AS b
INNER JOIN wheel AS wh
ON b.id_wheelfk = wh.id_wheel
INNER JOIN detailassembly AS da
ON barbell_idfk = b.barbell_id ) t
JOIN car AS c
ON t.id_carfk = c.id_car
AND t.id_carfk = c.id_car