使用UNION和WHERE多个表

时间:2018-04-06 08:21:24

标签: mysql sql

$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");

这是我的代码,我不知道我的错在哪里,有人可以帮我纠正这段代码吗?我很困惑。

这是我的数据库

enter image description here

我想制作像这样的输出

|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;
    }

2 个答案:

答案 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_cardescription),则只能使用子查询

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)

您必须更正查询中的许多内容:

  1. 您忘记了2个子查询中的SELECT关键字
  2. 联合查询必须具有相同数量和类型的结果列
  3. 子查询必须具有别名
  4. 您不能在WHERE CLAUSE
  5. 之后使用FROM
  6. 您必须在子查询中选择id_carfk才能与外部查询中的CAR表进行连接
  7. 这是一个格式良好的查询

    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