简体   繁体   中英

mysql group by and order by with limit query

mysql table structure is as follows

cmpid cmpname empid empname join-date
    1     xxx    21      p        18
    2     abc    13      q        10
    1     xxx    20      r         9      
    2     abc    19      s        21
    2     abc    18      t        20
    1     xxx    19      u         1

I want result as per format

cmpid cmpname empid empname join-date
    2     abc    19      s         21
    2     abc    18      t         20

    1     xxx    21      p         18
    1     xxx    20      r          9

means which ever companies has any new emp. then it return that company first with first two joined emp info. and then other compnies with same format.

means order on join date and group on cmpname with 2 people info

If any one knows please reply

Thanks

Did you mean this?

select c.cmpid, c.cmpname, e.empid, e.empname, e.join-date
from company c
inner join employee e on (c.cmpid = e.cmpid)
group by c.cmpid, e.emp_id 
order by e.join-date

You need to split that table into two different tables, employees and companies, with another table to join them together. With your current structure most things you want to do will be impossible.

I strongly recommend reading up on the rules of database normalisation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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