我有一个非常糟糕的表结构的旧数据库,我正在尝试创建具有更好结构的这些表。为此,我需要匹配两个表,以获取类别的ID。
这是我的两张旧表:
表格分类:
| ID | catname | cat1 | cat2 | cat3 | cat4 |
+----+--------------+--------+-------------+---------+------+
| 1 | bike | bike | NULL | NULL | NULL |
| 2 | accessories | bike | accessories | NULL | NULL |
| 3 | helmets | bike | accessories | helmets | NULL |
| 4 | lights | bike | accessories | lights | NULL |
| 5 | led | bike | accessories | lights | led |
表产品:
| ID | productnr | productname | cat1 | cat2 | cat3 | cat4 |
+----+-------------+---------------+-------+-------------+---------+------+
| 1 | 451157 | productya | bike | accessories | NULL | NULL |
| 2 | 555523 | product11 | bike | accessories | helmets | NULL |
| 3 | 234432 | helmetxqa | bike | accessories | helmets | NULL |
| 4 | 666623 | lightblue | bike | accessories | lights | NULL |
| 5 | 542123 | foobarlight | bike | accessories | lights | led |
首先,我想从产品表中删除列cat1,2,3和4.
所以我得到这样的结果:
| ID | catId | productnr | productname |
+----+---------+------------+---------------+
| 1 | 2 | 451157 | productya |
| 2 | 3 | 555523 | product11 |
| 3 | 3 | 234432 | helmetxqa |
| 4 | 4 | 666623 | lightblue |
| 5 | 5 | 542123 | foobarlight |
有人可以告诉我,我应该如何查询是否所有4只猫都匹配,然后给我猫咪?我已经尝试过这种方式了,但我认为这是错误的方式,因为每次产品只有2或3只猫,我都没有得到相关的catId。因此它仅适用于定义了所有4种猫的产品。
SELECT
cat.`id`,
prod.`productnr`,
prod.`productname`
FROM
products as prod
LEFT JOIN
categorys as cat
ON
cat.`cat1` = prod.`cat1`
AND
cat.`cat2` = prod.`cat2`
AND
cat.`cat3` = prod.`cat3`
AND
cat.`cat4` = prod.`cat4`
如果有人也对我有用,请告诉我。 ; - )
感谢您帮助我:)
答案 0 :(得分:3)
您可以通过为每个类别组合执行单独的连接来实现此目的。当然,这是一个等级排名,你不想要重复。因此,以下是这些检查:
SELECT prod.id, coalesce(c4.id, c3.id, c2.id, c1.id) as catid
prod.`productnr`, prod.`productname`
FROM products prod left join
categorys c4
on c4.cat1 = prod.cat1 and c4.cat2 = prod.cat2 and
c4.cat3 = prod.cat3 and c4.cat4 = prod.cat4 left join
categorys c3
on c3.cat1 = prod.cat1 and c3.cat2 = prod.cat2 and
c3.cat3 = prod.cat3 and c3.cat4 is null and
c4.id is null left join
categorys c2
on c2.cat1 = prod.cat1 and c2.cat2 = prod.cat2 and c2.cat3 is null and
c3.id is null and c4.id is null left join
categorys c1
on c1.cat1 = prod.cat1 and c1.cat2 is null and
c2.id is null and c3.id is null and c4.id is null;
在某些情况下,这可能会产生重复的行(尽管它可以避免这种情况)。如果发生这种情况,则可能仍需要group by
。
答案 1 :(得分:2)
首先,您需要正确的数据模型。
分类
| ID | catname |
+----+--------------+
产品:
| ID | productnr | productname |
+----+-------------+---------------+
哦,这么神奇的连接表:
|ID | product_id | category_id|
+---+------------+------------+
现在您可以正确地要求您的数据库为您提供数据
SELECT
cat.`id`,
prod.`productnr`,
prod.`productname`
FROM categories as cat
INNER JOIN products2categories p2c
ON p2c.category_id = cat.id
INNER JOIN products prod
ON p2c.product_id = prod.id
由于我真的不喜欢发放完整的复制粘贴示例,我将让您使用上面的查询来检查如何找到存在于4个类别中的产品。请记住 - 最简单的方法通常始终是正确的方法。
答案 2 :(得分:1)
问题可能是表中的null,因为sql
中的null!= null要解决这个问题,您可以合并数据
SELECT
cat.`id`,
prod.`productnr`,
prod.`productname`
FROM
products as prod
LEFT JOIN
categorys as cat
ON
coalesce(cat.`cat1`,'No Value') = coalesce(prod.`cat1`,'No Value')
AND
coalesce(cat.`cat2`,'No Value') = coalesce(prod.`cat2`,'No Value')
AND
coalesce(cat.`cat3`,'No Value') = coalesce(prod.`cat3`,'No Value')
AND
coalesce(cat.`cat4`,'No Value') = coalesce(prod.`cat4`,'No Value')
答案 3 :(得分:0)
如果您想在每个id
字段加入时仅获得cat
次,请尝试使用INNER JOIN
代替LEFT JOIN
。
SELECT
cat.`id`,
prod.`productnr`,
prod.`productname`
FROM
products as prod
INNER JOIN
categorys as cat
ON
cat.`cat1` = prod.`cat1`
AND
cat.`cat2` = prod.`cat2`
AND
cat.`cat3` = prod.`cat3`
AND
cat.`cat4` = prod.`cat4`