用MySQL中的中间表查询

时间:2019-02-11 07:46:12

标签: php mysql

我正在尝试使用包含非常重要的数据(称为“数量”的字段)的中间表进行查询,但无法显示我需要的所有数据

表是

TP_PANTONE_COLORS

id_pantone_color(主键)

name_pantone_color

hex_pantone_color


TP_COLOR_CART

id_color_cart(主键)

name_color_cart

说明

链接


TP_PANTONE_BASE(中级)

id_quantity(主键)

id_pantone_color

id_color_cart

数量


我需要一个这样的桌子

NAME_PANTONE_COLOR | NAME_COLOR_CART |数量|链接

我正在尝试

SELECT * FROM tp_color_cart A
INNER JOIN tp_pantone_base B ON        
A.id_color_cart = B.id_color_cart 
INNER JOIN tp_pantone_colors C ON  
C.id_pantone_color = B.id_pantone_color 
WHERE id_pantone_color=1

SELECT 
A.name_pantone_color    AS 'NAME_PANTONE_COLOR' ,
C.name_color_cart       AS 'NAME_COLOR_CART',
B.quantity              AS 'QUANTITY',
C.link                  AS 'LINK'
FROM tp_pantone_colors A
     JOIN tp_pantone_base B  ON B.id_pantone_color=A.id_pantone_color
     INNER JOIN tp_color_cart   C  ON B.id_color_cart=C.id_color_cart 
WHERE A.id_pantone_color=1

但是此查询返回一行,并且TP_PANTONE_BASE有4行,且id_pantone_color = 1

1 个答案:

答案 0 :(得分:0)

我无法重现您的问题。给定适当的数据,即使TP_PANTONE_BASE和tp_color_cart中只有1条记录,我也希望您的查询返回4行 例如

drop table if exists tp_pantone_colors,TP_PANTONE_BASE,TP_COLOR_CART;
create table TP_PANTONE_COLORS(
id_pantone_color int,
name_pantone_color varchar(1),
hex_pantone_color int
);

insert into TP_PANTONE_COLORS values
(1,'a',1),(1,'a',1),(1,'a',1),(1,'a',1);

create table TP_COLOR_CART(
id_color_cart int,
name_color_cart varchar(1),
description varchar(2),
link int
);
insert into tp_color_cart values
(1,'b','bb',1);

create table TP_PANTONE_BASE (
id_quantity int,
id_pantone_color int,
id_color_cart int,
quantity int
);

insert into TP_PANTONE_BASE values
(1,1,1,10);

select A.name_pantone_color    AS 'NAME_PANTONE_COLOR' ,
C.name_color_cart       AS 'NAME_COLOR_CART',
B.quantity              AS 'QUANTITY',
C.link                  AS 'LINK' 
from tp_pantone_colors a
join TP_PANTONE_BASE b on b.id_pantone_color = a.id_pantone_color
join TP_COLOR_CART c on c.id_color_cart = b.id_color_cart;

+--------------------+-----------------+----------+------+
| NAME_PANTONE_COLOR | NAME_COLOR_CART | QUANTITY | LINK |
+--------------------+-----------------+----------+------+
| a                  | b               |       10 |    1 |
| a                  | b               |       10 |    1 |
| a                  | b               |       10 |    1 |
| a                  | b               |       10 |    1 |
+--------------------+-----------------+----------+------+
4 rows in set (0.00 sec)

换句话说,如果没有您的样本数据,我们将无法解决这个问题。

根据您的数据

SELECT A.id_pantone_color,b.*
from tp_pantone_colors a
join TP_PANTONE_BASE b on b.id_pantone_color = a.id_pantone_color
WHERE A.id_pantone_color=1;

+------------------+-------------+------------------+---------------+----------+
| id_pantone_color | id_quantity | id_pantone_color | id_color_cart | quantity |
+------------------+-------------+------------------+---------------+----------+
|                1 |           1 |                1 |            30 | 13,693   |
|                1 |        2247 |                1 |           452 | 13,543   |
|                1 |        5616 |                1 |           453 | 2,55     |
|                1 |        7862 |                1 |           455 | 70,215   |
+------------------+-------------+------------------+---------------+----------+
4 rows in set (0.00 sec)

select * from TP_COLOR_CART where id_color_cart in(30,452,453,455)

+---------------+-----------------+-------------+---------+------+
| id_color_cart | name_color_cart | description | color   | link |
+---------------+-----------------+-------------+---------+------+
|            30 | 001             | White       | #FFFFFF | #    |
+---------------+-----------------+-------------+---------+------+
1 row in set (0.00 sec)

如果可以的话

select A.name_pantone_color    AS 'NAME_PANTONE_COLOR' ,
C.name_color_cart       AS 'NAME_COLOR_CART',
B.quantity              AS 'QUANTITY',
C.link                  AS 'LINK' 
from tp_pantone_colors a
join TP_PANTONE_BASE b on b.id_pantone_color = a.id_pantone_color
left join TP_COLOR_CART c on c.id_color_cart = b.id_color_cart
WHERE A.id_pantone_color=1;

+--------------------+-----------------+----------+------+
| NAME_PANTONE_COLOR | NAME_COLOR_CART | QUANTITY | LINK |
+--------------------+-----------------+----------+------+
| PANTONE 1605 C     | 001             | 13,693   | #    |
| PANTONE 1605 C     | NULL            | 13,543   | NULL |
| PANTONE 1605 C     | NULL            | 2,55     | NULL |
| PANTONE 1605 C     | NULL            | 70,215   | NULL |
+--------------------+-----------------+----------+------+
4 rows in set (0.02 sec)

但这不是您的查询错误,而是数据。