Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

My goal is to select rows from a mysql table, but according to certain column. Normal SELECT would be:

        SELECT
        ti.id, ti.userid, ti.date, ti.duedate, ti.datepaid, ti.subtotal, ti.taxrate, ti.paymentmethod, ti.notes AS invoicenotes,
        tii.invoiceid, tii.type, tii.description, tii.amount,
        tc.id, tc.firstname, tc.lastname, tc.companyname, tc.email, tc.address1, tc.address2, tc.city, tc.state,
        tc.postcode, tc.country, tc.phonenumber, tc.currency, tc.defaultgateway, tc.credit, tc.taxexempt,
        tc.latefeeoveride, tc.overideduenotices, tc.separateinvoices, tc.disableautocc, tc.datecreated,
        tc.notes AS tcnotes,
          (SELECT GROUP_CONCAT(value) FROM tblcustomfieldsvalues WHERE relid=tc.id) AS vatid
        FROM tblinvoices ti
        LEFT JOIN tblinvoiceitems tii
        ON tii.invoiceid=ti.id
        LEFT JOIN tblclients tc
        ON tc.id=tii.userid
        WHERE ti.status='Paid'
        AND ti.infakt_no IS NULL
        AND ti.paymentmethod='transferuj'
        ORDER BY ti.userid AND tii.id

But that kind of SELECT would split rows with same userid value. How to SELECT certain amount of rows and keep from separating userid?

share|improve this question
    
Please add your table structure and some data to explain your question.Also please clarify your question. –  Abdul Manaf Jul 18 '13 at 11:01
    
please show example output - both for the undesired and desired query results. This will clarify your question –  redguy Jul 18 '13 at 11:16

1 Answer 1

What i understand from your question is you need to group your result on the basis of user_id for that you can try this one

SELECT user_id,GROUP_CONCAT(id),GROUP_CONCAT(content) FROM tbl WHERE sth='sth' GROUP BY user_id LIMIT 10;

If you need anything else please comment and update in your question.

share|improve this answer
    
+1 for the GROUP_CONCAT –  redguy Jul 18 '13 at 11:21
    
This is a beautiful answer. Nice! –  dotancohen Jul 18 '13 at 12:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.