简体   繁体   English

一对多的MySQL查询

[英]MySQL Query with one-to-many

I am new to MySQL and need help with a query Here is some sample data with columns: 我是MySQL的新手,需要查询方面的帮助。这是一些带有列的示例数据:

modelid styleid sequence
19463   316811  0
19463   316812  1
19463   316813  2
19463   316814  3
19463   316920  4
19463   316921  5
19727   318010  0
19727   318011  2
19727   318012  1
19727   318013  3
19884   320053  0
19884   320054  1
19884   320055  2
19884   320056  3
19998   321302  0
19998   321303  1
19998   321304  2
19998   321305  3
19998   321306  4
19998   321307  5
19998   321308  6
19998   321309  7

I need to get one styleid for every modelid, preferably with a sequence of 0 or 1, but the sequence does not really matter, it can be random. 我需要为每个modelid获得一个styleid,最好使用0或1的序列,但是序列并不重要,它可以是随机的。

The Best result would be something like this, CANT USE sequence = 0 as a where clause though: 最好的结果是这样的,尽管CANT USE sequence = 0作为where子句:

modelid styleid sequence
19463   316811  0
19727   318010  0
19884   320053  0

Thanks 谢谢

Try this:: 尝试这个::

Either you can use group by like :: 您可以使用group by类的::

Select modelid, styleid, sequence from myTable group by modelid

or you can use DISTINCT 或者您可以使用DISTINCT

Select DISTINCT modelid, styleid, sequence from myTable

Try this 尝试这个

SELECT modelid, MAX(styleid),MIN(sequence)
FROM myTable 
GROUP BY modelid

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM