lundi 21 mars 2016

How can I attack this mySQL query?

I need to combine two tables.

The first table is People(SSN PRIMARY KEY, fname, lname).

The other table is Cities(name, cityID), where name = "fname lname" i.e. 2 columns of People concatenated with a space between. One person may have multiple cities associated with them, and a city may have more than one person (many - many).

I want to combine the two into a table PeopleCities(SSN, cityID). If no person is found for a city, I need SSN to be 0 for that cityID. My experience is mostly with sqlite rather than mySQL, so I'm not very confident in my query.

Here is my query:

SELECT ISNULL(People.SSN, 0), Cities.cityID
FROM People 
FULL OUTER JOIN Cities
ON (Cities.name = CONCAT_WS(" ", People.fname, " ", People.lname) FROM People);

Aucun commentaire:

Enregistrer un commentaire