[英]Sql group items with same value in same table in another column
The above image is an image of a resource table of a site am building an android app for.上图是正在为其构建 android 应用程序的站点的资源表的图像。 Please sorry for my bad english and poor explanation, i will try to explain to the best i can.
请原谅我糟糕的英语和糟糕的解释,我会尽力解释。 i have a table called resources as show below and it is part of another table called product.
我有一个名为资源的表,如下所示,它是另一个名为产品的表的一部分。 Product table is where the information about the product is stored while resource table is where images name and storage direction is stored.
产品表是存储产品信息的地方,而资源表是存储图片名称和存储方向的地方。 Each image has a seperate line.
每个图像都有单独的一行。 now if i try to run a query to link or join the product and resource table i start getting duplicated / repeated output values because of the images of the resources.
现在,如果我尝试运行查询以链接或加入产品和资源表,由于资源的图像,我开始获得重复/重复的输出值。
below is the product table下面是产品表
pk_i_id | fk_i_user_id | p_name | p_description | etc
1 4 iphone Iphone X
2 4 Cloth Gucci
3 6 watch Rolex Watch
4 3 car Venza
5 7 tractor CAT
6 9 Fruits Melon
below is the resource table下面是资源表
pk_i_id | fk_i_item_id | s_name | s_extension | s_content_type | s_path
1 2 xDSDki0F jpg image/jpeg oc-content/uploads/0/
2 2 GAsk8vqW jpg image/jpeg oc-content/uploads/0/
3 3 D033Znu1 jpg image/jpeg oc-content/uploads/0/
4 3 yZSPKeCj jpg image/jpeg oc-content/uploads/0/
5 5 rc8wCLOQ jpg image/jpeg oc-content/uploads/0/
6 5 tUWBh6vk jpg image/jpeg oc-content/uploads/0/
8 6 v3rSPl9s jpg image/jpeg oc-content/uploads/0/
9 7 8FTuKvgl jpg image/jpeg oc-content/uploads/0/
10 7 fzb2KzYq jpg image/jpeg oc-content/uploads/0/
11 7 9Qs7MgUl jpg image/jpeg oc-content/uploads/0/
12 8 3HmpjxC7 jpg image/jpeg oc-content/uploads/0/
13 8 VF4M6PrE jpg image/jpeg oc-content/uploads/0/
14 8 tnIH7LqU jpg image/jpeg oc-content/uploads/0/
15 8 M5jKALrD jpg image/jpeg oc-content/uploads/0/
16 9 vBBSSfoD jpg image/jpeg oc-content/uploads/0/
if i do a Query like this如果我做这样的查询
select * products p, resource r where p.pk_i_id=r.fk_i_item_id
It will give me Multiple repeated Query due to multiple section of resource image storage.由于资源图像存储的多个部分,它会给我多次重复查询。 if Image about Car is 3 the query will display repeated query of car three times.
如果 Image about Car 为 3,则查询将显示重复查询汽车 3 次。
Judging from the above table if i try to fetch from here using this below Query"从上表判断,如果我尝试使用下面的查询从这里获取”
select * from osoe_t_item_resource
it will display all those above table value in the app.它将在应用程序中显示所有上述表格值。
Please i need a query that will group the items with the same fk_i_item_id as another column example请我需要一个查询,它将与另一个列示例具有相同 fk_i_item_id 的项目分组
instead of appearing like this output below而不是像下面这样的输出
{"NewsApp":[
{"pk_i_id":"1","fk_i_item_id":"2","s_name":"xDSDki0F
{"pk_i_id":"2","fk_i_item_id":"2","s_name":"GAsk8vqW
{"pk_i_id":"3","fk_i_item_id":"3","s_name":"D033Znu1"},
{"pk_i_id":"4","fk_i_item_id":"3","s_name":"yZSPKeCj"},
{"pk_i_id":"5","fk_i_item_id":"5","s_name":"rc8wCLOQ"},
{"pk_i_id":"6","fk_i_item_id":"5","s_name":"tUWBh6vk"},
{"pk_i_id":"8","fk_i_item_id":"6","s_name":"v3rSPl9s"},
{"pk_i_id":"9","fk_i_item_id":"7","s_name":"8FTuKvgl"},
{"pk_i_id":"10","fk_i_item_id":"7","s_name":"fzb2KzYq"},
{"pk_i_id":"11","fk_i_item_id":"7","s_name":"9Qs7MgUl"},
{"pk_i_id":"12","fk_i_item_id":"8","s_name":"3HmpjxC7"},
{"pk_i_id":"13","fk_i_item_id":"8","s_name":"VF4M6PrE"},
{"pk_i_id":"14","fk_i_item_id":"8","s_name":"tnIH7LqU"},
{"pk_i_id":"15","fk_i_item_id":"8","s_name":"M5jKALrD"},
{"pk_i_id":"16","fk_i_item_id":"9","s_name":"vBBSSfoD"},
{"pk_i_id":"17","fk_i_item_id":"9","s_name":"kUt65zVb"},
{"pk_i_id":"18","fk_i_item_id":"9","s_name":"D5DXAszv"},
it should appear like this output它应该看起来像这个输出
{"NewsApp":[
{"pk_i_id":"1","fk_i_item_id":"2","s_name":"xDSDki0F","s_name2":"GAsk8vqW"},
{"pk_i_id":"3","fk_i_item_id":"3","s_name":"D033Znu1","s_name2":"yZSPKeCj"},
{"pk_i_id":"5","fk_i_item_id":"5","s_name":"rc8wCLOQ","s_name2":"tUWBh6vk"},
{"pk_i_id":"8","fk_i_item_id":"6","s_name":"v3rSPl9s"},
{"pk_i_id":"9","fk_i_item_id":"7","s_name":"8FTuKvgl","s_name2":"fzb2KzYq","s_name3":"9Qs7MgUl"},
{"pk_i_id":"12","fk_i_item_id":"8","s_name":"3HmpjxC7"},
{"pk_i_id":"13","fk_i_item_id":"8","s_name":"VF4M6PrE","s_name2":"tnIH7LqU","s_name":"M5jKALrD"},
{"pk_i_id":"16","fk_i_item_id":"9","s_name2":"vBBSSfoD"},
{"pk_i_id":"17","fk_i_item_id":"9","s_name":"kUt65zVb","s_name2":"D5DXAszv"},
If you watch closely all times with the same id has be merged together in one out,如果你仔细观察所有具有相同 id 的时间已经合并在一起,
Please help me out.请帮帮我。
I understant that, when there are records with the same fk_i_item_id
, you want to keep only one, which has the lowest pk_i_id
.我明白,当存在具有相同
fk_i_item_id
记录时,您只想保留一个具有最低pk_i_id
。
If you are using MySQL 8.0, this is straightforward with window function ROW_NUMBER()
:如果您使用的是 MySQL 8.0,这对于窗口函数
ROW_NUMBER()
很简单:
SELECT x.pk_i_id, x.fk_i_item_id, x.s_name
FROM (
SELECT
r.*,
ROW_NUMBER() OVER(PARTITION BY r.fk_i_item_id ORDER BY r.pk_i_id) rn
FROM resource r
) x WHERE x.rn = 1
The inner query ranks each record within fk_i_item_id
groups, and the outer query filters out duplicates.内部查询对
fk_i_item_id
组中的每条记录进行fk_i_item_id
,外部查询过滤掉重复项。
If you are running an earlier version of MySQL, you can use a NOT EXIST
condition with a correlated subquery that ensures that no other record exists with the same fk_i_item_id
and a lower pk_i_id
:如果您运行的是较早版本的 MySQL,则可以将
NOT EXIST
条件与相关子查询一起使用,以确保不存在具有相同fk_i_item_id
和较低pk_i_id
其他记录:
SELECT r.pk_i_id, r.fk_i_item_id, r.s_name
FROM resource r
WHERE NOT EXISTS (
SELECT 1
FROM resource r1
WHERE r1.fk_i_item_id = r.fk_i_item_id AND r1.pk_i_id < r.pk_i_id
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.