简体   繁体   中英

using UNION and WHERE with multiple table

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

that is my code, and I don't know where is my fault, can anyone help me to correct this code? I'm confused.

and this is my database

在此处输入图片说明

I want make an output like this

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

please, help me with my code. I can't run it.

EDIT : this is my error Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in C:\\xampp\\htdocs\\hwbase\\PHP\\connect.php on line 17

and this is my function

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

Try this query

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

If you don't need any information from car (for example pn_car , description ) you can use only the subquery

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

You have to correct many things in your query :

  1. You forgot SELECT keyword inside the 2 subqueries
  2. Union query must have the same number and type of result columns
  3. A subquery must have an alias
  4. You cannot use FROM after a WHERE CLAUSE
  5. You have to select the id_carfk inside subqueries in order to perform a join with the CAR table in the outer query

Here is a query wich is well formated

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM