Hi There,
I am using a sql query to show a row to users.
The Problem is in the database there are single as well as duplicate records.
My 1st code is – Works good if user has no duplicate records;
SELECT
contacts.forenames, contacts.surname, addresses.postcode, contacts.contact_number
FROM
contacts
LEFT JOIN addresses
ON contacts.address_number=addresses.address_number
WHERE
contacts.surname='$sn' AND contacts.forenames='$fn' AND addresses.postcode='$ad'
My 2nd code is – Works good if user has duplicate records;
SELECT
contacts.forenames, contacts.surname, communications.number, contacts.contact_number
FROM
contacts
LEFT JOIN communications
ON contacts.contact_number=communications.contact_number
WHERE
contacts.surname='$sn' AND contacts.forenames='$fn' AND communications.number='$em'
AND contacts.contact_number = (SELECT MAX(contacts.contact_number) FROM contacts)
But I need to use these in one – something like that => if user is single record use this
WHERE
contacts.surname='$sn' AND contacts.forenames='$fn' AND communications.number='$em'
Or use this (to print highest number)
WHERE
contacts.surname='$sn' AND contacts.forenames='$fn' AND communications.number='$em'
AND contacts.contact_number = (SELECT MAX(contacts.contact_number) FROM contacts)
Any Ideas?
Thank You!