0

I have a table like this:

id transaction_id auto_recurring paid_amount package_customerid
37              0              1           0                  4
45             37              1           0                  4
51              0              1           0                  4
57             51              1           0                  4
62              0              1           0                  4
67             62              1           0                  4

There are 6 records of package_customer_id = 4. Now I want to get the last record of 4. in this case id = 67 is my desired record. I try this SELECT * FROM transactions GROUP BY package_customer_id. But I got first record of package_customer_id = 4. i.e: id = 4 is my fetched result. How can I get id = 67 (my desired record) modifying this sql?

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Abdus Sattar Bhuiyan
  • 3,016
  • 4
  • 38
  • 72
  • Be aware that when using "Group By" you aren't necessarily getting a record back. You will get a collection of fields that match the selection criteria. The fields in the selection (where your "*" is) need to either be represented in a where clause or be using aggregate functions like MAX https://dev.mysql.com/doc/refman/5.7/en/group-by-functions.html Newer version of MySQL will give you an error if you try to do this. – Jaydee Jul 25 '17 at 13:17

5 Answers5

2

SELECT * FROM transactions WHERE package_customer_id = 4 ORDER BY id DESC LIMIT 1;

That would be my shot at it. Sorry but i haven't tested it, i leave it up to you:)

EDIT:

Dont forget the quotes " ` " on columns name's:)

Check you column name package_customer_id OR package_customerid ?

0

Don't use group by. Use where:

SELECT t.*
FROM transactions t
WHERE t.id = (SELECT MAX(t2.id)
              FROM transactions t2
              WHERE t2.package_customer_id = t.package_customer_id
             );

You can filter this for whichever package customer ids you like, in the outer query.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

You may try this .

SELECT temp.* FROM (SELECT * FROM `transactions` WHERE package_customer_id = 4 order by id DESC LIMIT 1 ) AS temp GROUP BY temp.package_customer_id
vjy tiwari
  • 843
  • 1
  • 5
  • 17
-1

You can use like this:

SELECT * FROM transactions WHERE id IN (SELECT MAX(id) FROM transactions GROUP BY package_customerid)
Omar Faruk
  • 298
  • 5
  • 19
-1

try this query

if you need with GROUP BY clause: use this query

SELECT * FROM transactions where GROUP BY package_customerid ORDER BY package_customerid DESC LIMIT 1;

OR if you DON'T need with GROUP BY clause: use this query

SELECT MAX(id),transaction_id,auto_recurring,paid_amount,package_customerid FROM transactions where package_customerid=4; 

Output:

id transaction_id auto_recurring paid_amount package_customerid
67             62              1           0                  4
RaMeSh
  • 3,330
  • 2
  • 19
  • 31
  • In the question , mentioned that use Group by clause – vjy tiwari Jul 25 '17 at 13:38
  • I never understood using aggregation w/o a group by when the field values were different. so what transaction_ID would be selected here? why is it 62? and not 0 51 or 37? – xQbert Jul 25 '17 at 13:53