Try:
Code: Select all
SELECT a.*, c.Contactdetails FROM Addresses AS a LEFT JOIN Contacts AS c ON a.Recordid = c.Recordid
WHERE a.Firstname LIKE '%gary%' ORDER BY Displayas
or
Code: Select all
SELECT DISTINCT a.*, c.Contactdetails FROM Addresses AS a LEFT JOIN Contacts AS c ON a.Recordid = c.Recordid
WHERE a.Firstname LIKE '%gary%' ORDER BY Displayas
Explanation:
SQL is creating a "virtual" table by joining both tables using left join (left table = check all records, right table = only add records where commom field is the same), and to do so you must define a common field on both tables (Recordid in your case), after this 'union' you apply your filters ( "'%gary%'" in this case).
The result is an output with the fields you selected from both tables, and filtered by your options.
The Distinct option is sometimes usefull to remove duplicate "equal" records, so the output only shows one distinct result of each record (this usually happens when you group records with the GROUP BY option).
-- EDIT --
Code: Select all
SELECT Firstname, Middlename, Lastname, Business, Nickname, Customname, Street, Suburb, City, State, Postcode, Country, Displayas, Ownername, Category, Comment FROM Addresses
WHERE
Firstname LIKE '%gary%'
OR Middlename LIKE '%gary%'
OR Lastname LIKE '%gary%'
OR Business LIKE '%gary%'
OR Nickname LIKE '%gary%'
OR Customname LIKE '%gary%'
OR Street LIKE '%gary%'
OR Suburb LIKE '%gary%'
OR City LIKE '%gary%'
OR State LIKE '%gary%'
OR Postcode LIKE '%gary%'
OR Country LIKE '%gary%'
OR Displayas LIKE '%gary%'
OR Ownername LIKE '%gary%'
OR Category LIKE '%gary%'
UNION
SELECT a.Firstname, a.Middlename, a.Lastname, a.Business, a.Nickname, a.Customname, a.Street, a.Suburb, a.City, a.State, a.Postcode, a.Country, a.Displayas, a.Ownername, a.Category, a.Comment
FROM Addresses AS a, Contacts AS c
WHERE c.Contactdetails LIKE '%gary%' AND a.Recordid = c.Recordid
OMG
Well it works, but it would crash or slow down a mysql server if you ever did that query on a few millions records
It's like killing a flye with a bazooka !!!
Here's my version
Code: Select all
SELECT * FROM Addresses AS a LEFT JOIN Contacts AS c ON a.Recordid = c.Recordid
WHERE a.Firstname OR a.Middlename OR a.Lastname OR a.Business OR a.Nickname OR a.Customname OR a.Street OR a.Suburb
OR a.City OR a.State OR a.Postcode OR a.Country OR a.Displayas OR a.Ownername OR a.Category or c.Contactdetails LIKE '%gary%' ORDER BY Displayas