473,387 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to select last entry when joining?

Hello,
I have a query like this:
Expand|Select|Wrap|Line Numbers
  1. SELECT jos_vm_orders.user_id, jos_vm_orders.cdate, sum( order_total ) AS total, count( jos_vm_orders.order_id ) AS ordercount, jos_vm_user_info.first_name, jos_vm_user_info.last_name, jos_vm_user_info.phone_1, jos_vm_user_info.user_email, jos_vm_user_info.city
  2. FROM jos_vm_orders
  3. LEFT JOIN jos_vm_user_info ON jos_vm_orders.user_id = jos_vm_user_info.user_id
  4. WHERE jos_vm_orders.order_status = 'C'
  5. GROUP BY jos_vm_orders.user_id
  6. LIMIT 0 , 30
When there is more than one order for same user_id the script selects the first order cdate, but I would like to select the last one. Any ideas?
Jul 19 '10 #1
2 2383
code green
1,726 Expert 1GB
This actually an illegal query except MySQL uses an algorithm to 'guess' the fields you require where you haven't used an aggregate or a GROUP BY.

So jos_vm_orders.cdate, jos_vm_user_info.first_name, jos_vm_user_info.last_name, jos_vm_user_info.phone_1, jos_vm_user_info.user_email, jos_vm_user_info.city are all chosen for you.

To specify the last date you could try
MAX() or use HAVING.
Jul 19 '10 #2
Well all other information for one user_id ir the same, but the cdate differs. Thank you very much for MAX(), this solved my probled because date is stored in timestamp format.
Jul 19 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: mr_burns | last post by:
hi, i have two tables that i am joining using the following sql: SELECT * FROM clients, programmes WHERE clients.client_id = programmes.client_id AND programmes.client_id = 12 if both...
0
by: Toby | last post by:
Dear All, I wounder whether some of the experts out there might be able to help me with a problem I'm having. I do not know whether this is possible or not... I have a large table of stock...
3
by: eric404 | last post by:
(I don't post here often, so in case I'm violating long-standing taboos of this newsgroup, I apologize in advance for calling a relation a table, using nulls, and other ignorant, destructive, and...
0
by: Palle Girgensohn | last post by:
Hi! A performance question: I have some tables: Tabell "public.person" Kolumn | Typ | Modifierare ------------------+--------------------------+---------------...
2
by: PC | last post by:
Hi, I have used the DLast("!","")function to find and display the last entry in a table. How would I go about displaying the last record in a table that is not Null? Thanks in advance ...pc
11
by: David Messner | last post by:
Ok I know this is simple but the statement eludes me... I have a date field where I want the default value on the data entry form's date field to be the last date entered. I figure I can do this...
5
by: John Veldthuis | last post by:
My code works perfectly 100% when adding items to my ArrayList and updating the listbox. Works perfectly when deleting an item in the ArrayList when it is not the last entry but if it is the last...
1
by: Sakakini | last post by:
How can I append last entry from one table to another table???
5
by: Wernerh | last post by:
Hi, I have created a log in a listbox to show users everytime someone has logged on. Is there a form load function that will scroll down the listbox to the last entry each time the form is loaded?? ...
1
by: karuppiah | last post by:
Hi All, i need query,how to select last 3 month ,last 3wek,last 3 days , this last 3 month and 3 week should be friday, using sysdate from dual table, there is no exsting table, pls any one help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.