Ask a question about php + mysql

Source: Internet
Author: User
Ask a question about php + mysql to query data in a table with the condition uid in (, 3 ). Then, all the values that meet uid = 1, 2, or 3 are queried.
How can we get only one entry per condition?
For example, if the uid is 1, 2, and 3 respectively, you can use SQL to query only the first entry of uid 1, the first entry of uid 2, and the first entry of uid 3.


Reply to discussion (solution)

First, you must have an id and uid in your data.

Query the numbers of uid 1, 2, and 3 respectively: (assume that the table name is 'user', and the table has only two fields, id and uid, respectively)

select id,count(uid) from user group by uid having uid=1 or uid=2 or uid =3;


Query the first entry with uid 1:

select * from user where uid=1 limit 1;

You can obtain one query result set in the group.

Select * from tbl_name a where (select count (*) from tbl_name where uid = a. uid and id

Each condition has only one entry:
Select * from tt where uid in (1, 2, 3) group by uid;

Select. * from table a where 1> (select count (1) from table B where. id = B. id and. id> B. id) and. uid in (1, 2, 3)

Select top 1 * from table group by id

Group by can be implemented

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.