简体   繁体   中英

PHP MYSQL 5 table join issue

Attempting to get quantity remaining from subtraction of table from another based on matching column information

t1 (id,complete)

t2 (id,t1_id,qty,size,desc)

t3 (id,t1_id,qty,size,desc)

t4 (id,size)

t5 (id,desc)

t1 contains main "job" and if it is complete or not

t2 contains "items in"

t3 contains "items out"

t4 contains "size of item"

t5 contains "description of item"

example data:

t1 (id=300,complete=0)

t1 (id=350,complete=1)

t2 (id=1,t1_id=300,qty=20,size=1,desc=3)

t2 (id=2,t1_id=300,qty=10,size=2,desc=1)

t2 (id=3,t1_id=350,qty=10,size=2,desc=1)

t3 (id=1,t1_id=300,qty=7,size=1,desc=3)

t3 (id=2,t1_id=300,qty=9,size=2,desc=1)

t4 (id=1,size="3.5 inch")

t4 (id=2,size="4.5 inch")

t5 (id=1,desc="Drill")

t5 (id=3,desc="Flow")

expected output:

QTY: 13 3.5 inch Flow Remaining

QTY: 1 4.5 inch Drill Remaining

NOTICE that t1 id 350 is complete, and that's why it doesn't show.

$sql = "
SELECT
t1.id AS main_id,
t2.t1_id,
t2.qty,
t2.size,
t2.desc,
t3.t1_id,
t3.qty,
t3.size,
t3.desc,
t4.size,
t5.desc
SUM(t2.qty-t3.qty) AS remaining,
JOIN t2 ON t1.id=t2.t1_id
JOIN t3 ON t1.id=t3.t1_id
JOIN t4 ON t2.size=t4.id
JOIN t5 ON t2.desc=t5.id
JOIN t4 ON t3.size=t4.id
JOIN t5 ON t3.desc=t5.id            
WHERE           
(t1.complete != 1) AND (t2.size = t3.size) AND (t2.desc=t3.desc)        
ORDER BY t4.size ASC, t5.desc ASC ";
$result = $conn->query($sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
echo "$row['remaining']";
}
}

So i changed your sql statement, because that what you posted could not work.

CREATE TABLE t1 (
  id INT,
  complete int
 );
 INSERT INTO t1 (id,complete) VALUES 
   (300,0),(350,1);

CREATE TABLE t2 (
  id INT,
  t1_id int
  ,qty int
  ,size int
  ,desc1 int
);
INSERT INTO t2 (id,t1_id,qty,size,desc1) VALUES 
  (1,300,20,1,3),
  (2,300,10,2,1),
  (3,350,10,2,1);

CREATE TABLE t3 (
  id INT,
  t1_id int
  ,qty int
  ,size int
  ,desc1 int
);
INSERT INTO t3 (id,t1_id,qty,size,desc1) VALUES 
  (1,300,7,1,3),
  (2,300,9,2,1);

CREATE TABLE t4 (
  id INT,
  size varchar(100)
);
INSERT INTO t4 (id,size) VALUES 
  (1,"3.5 inch"),
  (2,"4.5 inch");

CREATE TABLE t5 (
  id INT,
  desc1 varchar(100)
);
INSERT INTO t5 (id,desc1) VALUES 
 (1,"Drill"),
 (3,"Flow")

The new Sql statement is

SELECT
  t1.id AS main_id,
  t2.t1_id,
  t2.qty,
  t2.size,
  t2.desc1
  ,t3.t1_id,
  t3.qty,
  t3.size,
  t3.desc1
 ,t4.size
 ,t5.desc1
 ,t2.qty-t3.qty AS remaining
FROM t1
  right JOIN t2 ON t1.id=t2.t1_id
  JOIN t3 ON t1.id=t3.t1_id and t2.size = t3.size
  JOIN t4 ON t2.size=t4.id
  JOIN t5 ON t2.desc1=t5.id     
WHERE           
  (t1.complete != 1)       
  ORDER BY t4.size ASC, t5.desc1 ASC; 

And the result is

main_id     t1_id   qty     size    desc1   t1_id   qty     size    desc1   size    desc1 remaining
 300        300     20       1       3       300    7       1        3      3.5 inch Flow   13
 300        300     10       2       1       300    9       2        1      4.5 inch Drill  1

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