多个INNER JOIN与WHERE CLAUSE - 4个表

时间:2015-02-08 17:38:09

标签: mysql

我加入的问题很小。我有4张桌子。

as_ticmat Tbl正在加入 as_mat Tbl,所以我可以从 as_mat Tbl获取名称列与<登记/> as_sertic Tbl正在加入 as_ser Tbl,但仅限于一个条件。来自 as_ticmat Tbl tic_id 列必须等于1 as_sertic Tbl tic_id 列必须等于1

这是我的表格

mysql> TBL: as_ticmat;
+--------------+---------+---------+-----------+
| ticmat_id    |  mat_id |  tic_id |  matprice |
+--------------+---------+---------+-----------+
|            1 |       1 |       1 |     20.00 |
|            2 |       2 |       1 |     30.00 |
|            3 |       3 |       4 |    640.00 |
|            4 |       4 |       4 |    500.00 |
+--------------+---------+---------+-----------+

mysql> TBL: as_mat;
+-----------+-------------------+
| mat_id    | name              |
+-----------+-------------------+
|         1 | XXXX - XXXXX - XX |
|         2 | XXXX - XXXXX - XX |
|         3 | XXXX - XXXXX - XX |
|         4 | XXXX - XXXXX - XX |
+-----------+-------------------+

mysql> TBL: as_sertic;
+--------------+---------+---------+---------+---------+
| sertic_id    |  tic_id |  ser_id |  mec_id | rate    |
+--------------+---------+---------+---------+---------+
|            1 |       1 |       2 |       4 |  500.00 |
|            2 |       1 |       3 |       4 | 1000.00 |
|            3 |       4 |       4 |       4 |  420.00 |
|            4 |       4 |       5 |       4 |  420.00 |
+--------------+---------+---------+---------+---------+

mysql> TBL: as_ser;
+------------+----------+--------+
| ser_id     | name     | price  |
+------------+----------+--------+
|          1 | XXX XXXX |  50.00 |
|          2 | XXX XXXX |  50.00 |
|          3 | XXX XXXX | 250.00 |
|          4 | XXX XXXX | 210.00 |
|          5 | XXX XXXX | 210.00 |
+------------+----------+--------+

这是我的查询。

SELECT `as_ticmat`.`tickmat_id`,
       `as_ticmat`.`mate_id`,
       `as_ticmat`.`mateprice`,
       `as_sertic`.`sertic_id`,
       `as_sertic`.`ser_id`,

SUM        (as_tictmat.matprice`) AS `mat`
SUM        (as_sertic.rate)       AS `ser`
FROM       `as_ticmat`
INNER JOIN `as_sertic`
ON         `as_sertic`.`ser_id` = `as_ser`.`ser_id`
INNER JOIN `as_mate`
ON         `as_ticmat`.`mat_id` = `as_mat`.`mat_id`
WHERE      `tic_id` = 1

1 个答案:

答案 0 :(得分:1)

您的查询有多个拼写错误,缺少逗号,您也从未加入as_ser表。我认为更正后的查询应如下所示:

SELECT as_ticmat.ticmat_id,
       as_ticmat.mat_id,
       as_ticmat.matprice,
       as_sertic.sertic_id,
       as_sertic.ser_id,
       SUM(as_ticmat.matprice) AS mat,
       SUM(as_sertic.rate)     AS ser
FROM       as_ticmat    
INNER JOIN as_sertic
ON         as_sertic.ser_id = as_ticmat.tic_id
INNER JOIN as_ser
ON         as_sertic.ser_id = as_ser.ser_id
INNER JOIN as_mat
ON         as_ticmat.mat_id = as_mat.mat_id
WHERE      as_ticmat.tic_id = 1

从出现的错误消息中很容易找到所有这些错误......

对于大多数SQL数据库,您最后还需要一个group by子句,因为您正在使用聚合函数(MySQL是一个例外,因为它不需要group by子句,但是你赢了' t得到你对数据进行分组时的结果)在这种情况下你应该添加

GROUP BY 
       as_ticmat.ticmat_id,
       as_ticmat.mat_id,
       as_ticmat.matprice,
       as_sertic.sertic_id,
       as_sertic.ser_id

在查询结束时。