SQL Query Filtering - PLease Help, Thank you

TABLEI have a table with data R. Virgo (3 Rows). I want to query the most recent only the frist row and not all the data with this code:

    Private Sub DISPLAY()
        conn.Close()
        Dim cmd2 As New MySqlCommand
        Dim myDA2 As New MySqlDataAdapter(cmd2)
        Dim myDT2 As New DataTable
        cmd2.Connection = conn
        cmd2.CommandText = "SELECT * FROM esd_reco where IDNumber = '" & txtG.Text & "' ORDER BY Date DESC"
        myDA2.Fill(myDT2)
        dghistory.DataSource = myDT2
        conn.Close()

Open in new window


txtG.text is the ID Number
Help please, thank you.
Rowel VirgoVisual Studio .NETAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pawan KumarDatabase ExpertCommented:
Please try this -

cmd2.CommandText = "SELECT TOP 1 * FROM esd_reco where IDNumber = '" & txtG.Text & "' ORDER BY Date DESC"
0
Pawan KumarDatabase ExpertCommented:
Or this -

cmd2.CommandText = "SELECT Name,Department,User_Type,MAX(date) date,MAX(IdNumber)IdNumber FROM esd_reco where IDNumber = '" & txtG.Text & "' ORDER BY Date DESC" & "GROUP BY Name,Department,User_Type"
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
are you using MySQL as your database? If yes then Top 1 will not worked for you.

you can try use LIMIT clause:

SELECT * FROM msd.esd_reco where IDNumber = '1504579' ORDER BY Date DESC LIMIT 1

Open in new window


or if you selecting the latest record for each grouping, try:

SELECT a.* FROM msd.esd_reco a
inner join 
(
	select IDNumber, max(date) date from msd.esd_reco group by IDNumber
) b
on a.IDNumber = b.IDNumber and a.date = b.date

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
R

From novice to tech pro — start learning today.