两个表之间的SQL连接

时间:2016-06-09 08:50:55

标签: mysql sql join left-join

以下是我的两个表结构,

mysql> desc catalog_product_entity;
+------------------+----------------------+------+-----+---------------------+----------------+
| Field            | Type                 | Null | Key | Default             | Extra          |
+------------------+----------------------+------+-----+---------------------+----------------+
| entity_id        | int(10) unsigned     | NO   | PRI | NULL                | auto_increment |
| entity_type_id   | smallint(8) unsigned | NO   | MUL | 0                   |                |
| attribute_set_id | smallint(5) unsigned | NO   | MUL | 0                   |                |
| type_id          | varchar(32)          | NO   |     | simple              |                |
| sku              | varchar(64)          | YES  | MUL | NULL                |                |
| created_at       | datetime             | NO   |     | 0000-00-00 00:00:00 |                |
| updated_at       | datetime             | NO   |     | 0000-00-00 00:00:00 |                |
| has_options      | smallint(1)          | NO   |     | 0                   |                |
| required_options | tinyint(1) unsigned  | NO   |     | 0                   |                |
| user_id          | mediumint(11)        | NO   |     | NULL                |                |
+------------------+----------------------+------+-----+---------------------+----------------+
10 rows in set (0.00 sec)

mysql> desc catalog_product_entity_varchar;
+----------------+-----------------------+------+-----+---------+----------------+
| Field          | Type                  | Null | Key | Default | Extra          |
+----------------+-----------------------+------+-----+---------+----------------+
| value_id       | int(11)               | NO   | PRI | NULL    | auto_increment |
| entity_type_id | mediumint(8) unsigned | NO   |     | 0       |                |
| attribute_id   | smallint(5) unsigned  | NO   | MUL | 0       |                |
| store_id       | smallint(5) unsigned  | NO   | MUL | 0       |                |
| entity_id      | int(10) unsigned      | NO   | MUL | 0       |                |
| value          | varchar(255)          | NO   |     |         |                |
+----------------+-----------------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

我想根据以下标准加入两个表,

我想从第二个表catalog_product_entity_varchar中获取所有值,其中entity_id应与第一个表catalog_product_entity匹配,并且第一个表中的user_id为12

以下是我的SQL查询 -

select entity_id, value, COUNT(*) as count 
    from catalog_product_entity_varchar as cpev 
    INNER JOIN catalog_product_entity as cpe on cpe.entity_id = cpev.entity_id 
    where attribute_id = '960' 
    GROUP BY value 
    HAVING (COUNT(*) > 1) 
    limit 1

更新 -

我更新了我的sql查询, 但它只显示一个结果而不是多个结果。

select catalog_product_entity_varchar.entity_id,
       catalog_product_entity_varchar.value, COUNT(*) as count,
       catalog_product_entity.entity_id, catalog_product_entity.user_id
from catalog_product_entity_varchar,
     catalog_product_entity
where attribute_id = '960'
  and catalog_product_entity_varchar.entity_id = catalog_product_entity.entity_id
  AND catalog_product_entity.user_id = '12'
GROUP BY value
HAVING (COUNT(*) > 1)
limit 1;

当前结果 -

+-----------+---------------+-------+-----------+---------+
| entity_id | value         | count | entity_id | user_id |
+-----------+---------------+-------+-----------+---------+
|  11062505 | 05-04100262-R |     2 |  11062505 |      12 |
+-----------+---------------+-------+-----------+---------+
1 row in set (36.67 sec)

预期结果 -

+-----------+----------------+--------------+----------+-----------+---------------+
| value_id  | entity_type_id | attribute_id | store_id | entity_id | value         |
+-----------+----------------+--------------+----------+-----------+---------------+
|  63606647 |             10 |          960 |        0 |  11062505 | 05-04100262-R |
| 149826537 |             10 |          960 |        0 |  19987372 | 05-04100262-R |
+-----------+----------------+--------------+----------+-----------+---------------+
2 rows in set (12.84 sec)

我们也可以优化此查询,因为当前查询花费了太多时间

1 个答案:

答案 0 :(得分:0)

您似乎需要的是一个简单的连接,远比您发布的示例SQL简单。

这样的事情: -

SELECT cpev.value_id,
        cpev.entity_type_id,
        cpev.attribute_id,
        cpev.store_id,
        cpev.entity_id,
        cpev.value
FROM catalog_product_entity_varchar as cpev 
INNER JOIN catalog_product_entity as cpe on cpe.entity_id = cpev.entity_id 
WHERE  cpe.user_id = 12

但我怀疑你错过了你的要求。

修改

我认为您要做的是查找多次出现的所有值,然后恢复具有该值的记录。如果是这样,请使用子查询来获取多次使用的值,然后再次将其与主表连接: -

SELECT cpev.value_id,
        cpev.entity_type_id,
        cpev.attribute_id,
        cpev.store_id,
        cpev.entity_id,
        cpev.value
FROM catalog_product_entity_varchar cpev 
INNER JOIN catalog_product_entity cpe ON cpe.entity_id = cpev.entity_id 
INNER JOIN
(
    SELECT cpev.value, 
            COUNT(*) AS value_count 
    FROM catalog_product_entity_varchar cpev 
    INNER JOIN catalog_product_entity cpe ON cpe.entity_id = cpev.entity_id 
    WHERE cpev.attribute_id = '960' 
    AND  cpe.user_id = 12
    GROUP BY value 
    HAVING value_count > 1
) sub0
ON cpev.value = sub0.value
WHERE cpev.attribute_id = '960' 
AND  cpe.user_id = 12