I’ll fabricate data to illustrate my question:
I have 2 tables, one called clients, and one called shippingProfiles.
clients shippingProfiles
************************************* *********************************
* clientId username emailAddress * * clientId street town *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 1 james jdoer@aol.org * * 1 mole north *
* 2 zac zmit@aol.org * * 1 maple pleasant *
* 3 cris cm@yahoo.org * * 3 oak brook *
* 4 john jg@yahoo.org * * 3 taylor glen *
************************************* *********************************
clients.clientId is a unique primary key. Some of the clients have multiple shipping profiles, others have none.
I would like to search the database for any client that has the letter “m” in their username, emailAddress, street, or town. I would like each unique client to be returned as one entry (and only one entry), showing their id, username, email address, street, and town. If they don’t have a street and town it can display null (or something similar), and if they have multiple streets and towns it can display any one of them.
This should be the result:
*********************************************************
* clientId username emailAddress street town *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* 1 james jdoer@aol.org mole north *
* 2 zac zmit@aol.org null null *
* 3 cris cm@yahoo.org oak brook *
*********************************************************
The tables may eventually hold 50,000 entries each so I need the database to do this in the least possible sweeps.
I’ve heard mysql is actually very powerful so I’m assuming this can be done with one command.
I think you want a left outer join – this will attempt to join the row to all other rows, but if it didn’t do any join it still prints all the fields, just putting null in the ones that were from the other table.
select * from clients LEFT OUTER JOIN shippingProfiles ON clients.clientId = shippingProfiles.ID
WHERE clients.username LIKE ‘%m%’ OR –insert other conditions here