Mysql 从多个重复项中仅选择一条记录
Hello im使用下面的查询从数据库中选择一些元素Mysql 从多个重复项中仅选择一条记录,mysql,sql,Mysql,Sql,Hello im使用下面的查询从数据库中选择一些元素 SELECT *,c.slug AS pslug,d.slug AS catslug FROM uhhu_virtuemart_products as a LEFT JOIN uhhu_virtuemart_products_el_gr as d on a.virtuemart_product_id=d.virtuemart_product_id LEFT JOIN uhhu_virtuemart_product_catego
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_products as a
LEFT JOIN uhhu_virtuemart_products_el_gr as d
on a.virtuemart_product_id=d.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_product_categories as b
ON a.virtuemart_product_id = b.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON b.virtuemart_category_id=c.virtuemart_category_id
WHERE a.virtuemart_product_id = 508
问题是一个产品可以有很多类别,所以这个查询返回我3行,而我需要始终取1行作为输出
virtuemart\u产品\u类别:
我想要的是,当它在这里检查类别时:
左键将UHU\u virtuemart\u产品\u类别合并为b
在a.virtuemart\u product\u id=b.virtuemart\u product\u id上
写一个限制1语句
我尝试使用如下嵌套选择:
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_products as a
WHERE uhhu_virtuemart_product_id =
(SELECT virtuemart_product_id
from virtuemart_product_categories
where virtuemart_product_id=508 LIMIT 1)
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON a.virtuemart_category_id=c.virtuemart_category_id
LEFT JOIN uhhu_virtuemart_categories_el_gr as d
on c.virtuemart_category_id=d.virtuemart_category_id
就像这样:
SELECT *,c.slug AS pslug,d.slug AS catslug
FROM uhhu_virtuemart_product_categories as a
LEFT JOIN uhhu_virtuemart_products_el_gr as d
on a.virtuemart_product_id=d.virtuemart_product_id
LEFT JOIN uhhu_virtuemart_categories_el_gr AS c
ON a.virtuemart_category_id=c.virtuemart_category_id
WHERE a.virtuemart_product_id =(
SELECT virtuemart_product_id
from uhhu_virtuemart_product_categories as f
where f.virtuemart_product_id=508
LIMIT 1)
我也尝试过使用SELECT distinct
,但它也不起作用。我只知道SQL的基础知识,找不到类似的方法来帮助我解决这个问题,所以我非常感谢您的帮助
希望我说得够清楚。DISTINCT对所选的所有行都有效。右连接对大多数人来说太混乱了,所以我的建议是执行“主表左连接附加数据”。你想让我将右连接更改为左连接吗?你想用右连接实现什么?它连接新表(
d
)的所有行,即使是表a
中没有相应键的行。这就是你想要的吗?似乎我读错了,那么正确的连接是什么:S我要编辑它你也可以按virtuemart\u product\u id分组