我在MySQL中具有三个表结构,如下所示:
+-----------------------+ | Tables_in_resort_wear | +-----------------------+ | Category | | Categorysub | | Items | +-----------------------+
+-------------+------------------+---------------+ | ID_Category | Name_Category | Rank_Category | +-------------+------------------+---------------+ | 101 | resort wear bali | 1 | | 102 | bali clothing | 1 | | 103 | plus size resort | 3 | | 104 | bali wholesale | 4 | | 105 | bali dress | 5 | | 106 | bali caftan | 6 | +-------------+------------------+---------------+
+----------------+-------------+------------------------+------------------+ | ID_Categorysub | ID_Category | Name_Categorysub | Rank_Categorysub | +----------------+-------------+------------------------+------------------+ | 630 | 103 | plus size resortwear1 | 1 | | 625 | 101 | resort wear bali 1 | 1 | | 626 | 101 | resort wear bali 2 | 2 | | 627 | 102 | bali clothing 1 | 1 | | 628 | 102 | bali clothing 2 | 2 | | 629 | 103 | plus size resortwear2 | 2 | | 631 | 104 | bali wholesale1 | 1 | | 632 | 104 | bali wholesale2 | 2 | | 633 | 105 | bali dress 1 | 1 | | 634 | 105 | bali dress 2 | 2 | | 635 | 106 | bali caftan 1 | 1 | | 636 | 106 | balicaftan 2 | 2 | +----------------+-------------+------------------------+------------------+
+----------+-------------+----------------+----------------------+----------------------------------------+ | ID_Items | ID_Category | ID_Categorysub | Code_Items | Name_Items | +----------+-------------+----------------+----------------------+----------------------------------------+ | 2519 | 101 | 625 | jm4828 long kaftan | long kaftan resort wear dress | | 2520 | 101 | 625 | jm4828 long kaftan | juwita moon long kaftan | | 2521 | 101 | 625 | jm4828 long dress | jm4828 long tropical resort wear dress | | 2522 | 101 | 625 | jm4828 resort wear | jm4828 juwita moon bali tropical dress | | 2523 | 101 | 626 | jm4828 long kaftan | jm4828 long kaftan dress bali | | 2524 | 101 | 626 | jm4828 kaftan bali | jm4828 women kaftan bali design | | 2525 | 101 | 626 | jm4828 bali kaftan | jm4828 juwita moon bali kaftan design | | 2526 | 101 | 626 | jm4828 bali resortwe | jm4828 bali resort wear long dress | | 2527 | 102 | 627 | jm44dm bali women | jm44dm bali resort wear long dress | | 2528 | 102 | 627 | jm44dm bali women | jm44dm resort wear dress bali | | 2529 | 102 | 627 | jm44dm bali dress | jm44dm bali long dress design | | 2530 | 102 | 627 | jm44dm bali dress | jm44dm women bali dress | | 2531 | 102 | 628 | jm44dm bali dress | jm44dm women long dress bali design | | 2532 | 102 | 628 | jm44dm bali longdres | jm44dm bali long dress design | | 2533 | 102 | 628 | jm44dm resort wear | jm44dm resort wear long dress | | 2534 | 102 | 628 | jm44dm bali caftan | jm44dm bali caftan | | 2535 | 103 | 630 | jm18 plus size bali | juwitamoon women plus size | | 2536 | 103 | 630 | jm18 plus size bali | juwita moon women plus sizes bali | | 2537 | 103 | 630 | jm7002 bali plus siz | women plus size resort wear bali | | 2538 | 103 | 630 | jm7002 plus size bal | best plus size caftan bali | | 2539 | 103 | 629 | jm1333 bali plus siz | juwita moon bali plus size shirt dress | | 2540 | 103 | 629 | jm44dm plus size | juwita moon plus size long dress | | 2541 | 103 | 629 | jm18 plus size bali | made in bali women plus size caftan | | 2542 | 103 | 629 | jm7002 plus size bal | juwita moon plus size women caftan | +----------+-------------+----------------+----------------------+----------------------------------------+
我想显示这3个表中的一些数据,为此我使用了JOIN。
这是我的查询
SELECT items.id_items,
items.name_items,
category.name_category,
categorysub.id_categorysub,
categorysub.name_categorysub
FROM category
JOIN items
ON category.id_category = items.id_category
JOIN categorysub
ON category.id_category = categorysub.id_category
LIMIT 8
但是,结果在ID_Items中显示了一个双精度数据,如下所示:
+----------+----------------------------------------+------------------+----------------+--------------------+ | ID_Items | Name_Items | Name_Category | ID_Categorysub | Name_Categorysub | +----------+----------------------------------------+------------------+----------------+--------------------+ | 2519 | long kaftan resort wear dress | resort wear bali | 625 | resort wear bali 1 | | 2519 | long kaftan resort wear dress | resort wear bali | 626 | resort wear bali 2 | | 2520 | juwita moon long kaftan | resort wear bali | 625 | resort wear bali 1 | | 2520 | juwita moon long kaftan | resort wear bali | 626 | resort wear bali 2 | | 2521 | jm4828 long tropical resort wear dress | resort wear bali | 625 | resort wear bali 1 | | 2521 | jm4828 long tropical resort wear dress | resort wear bali | 626 | resort wear bali 2 | | 2522 | jm4828 juwita moon bali tropical dress | resort wear bali | 625 | resort wear bali 1 | | 2522 | jm4828 juwita moon bali tropical dress | resort wear bali | 626 | resort wear bali 2 | +----------+----------------------------------------+------------------+----------------+--------------------+
我想显示一些类似的数据,但ID_Items中不包含重复数据。我已经尝试使用DISTINCT,并且得到了与上述相同的数据。 我该如何选择最好的方法?
答案 0 :(得分:0)
尝试使用分组依据
SELECT items.id_items,
items.name_items,
category.name_category,
categorysub.id_categorysub,
categorysub.name_categorysub
FROM category
JOIN items
ON category.id_category = items.id_category
JOIN categorysub
ON category.id_category = categorysub.id_category
GROUP BY id_items
答案 1 :(得分:0)
首先您加入category
和categorysub
,然后可以同时加入items
。
SELECT items.id_items,
items.name_items,
category.name_category,
categorysub.id_categorysub,
categorysub.name_categorysub
FROM category
JOIN categorysub
ON category.id_category = categorysub.id_category
JOIN items
ON category.id_category = items.id_category
AND categorysub.ID_Categorysub = items.ID_Categorysub
LIMIT 8
但这是针对您具有复合主键的情况。
对于您的数据,我认为ID_Categorysub
是唯一的,因此实际上您只需要:
JOIN items
ON categorysub.ID_Categorysub = items.ID_Categorysub