3 Table Select with Logic Conditions - Error: Not unique tables/alias

I’ve never done a 3 table join before. I’m looking to return the names (members table) and addresses (address_primary table) of people on a mailing list (list_mailer table) using 3 tables.

So far I have this -

  	$sql = "SELECT 
				members.fname, 
				members.lname, 
				address_primary.addrs_1, 
				address_primary.addrs_2,
				address_primary.city,
				address_primary.state,
				address_primary.zip
				
				
	        FROM members, address_primary 
			INNER JOIN address_primary AS members.member_id = address_primary.members_id
			INNER JOIN list_mailer AS members.member_id = list_mailer.members_id
			WHERE members.active = 1
			AND list_mailer.auction = 1
			ORDER BY members.lname, members.fname";

Not unique table/alias: ‘members’

You need to alias the tables

[code]SELECT
m.fname,
m.lname,
ap.addrs_1,
ap.addrs_2,
ap.city,
ap.state,
ap.zip

FROM members m, address_primary ap[/code]

Let’s start with a proper inner join. Here is an example using two tables.

[php]$sql = “SELECT
m.fname,
m.lname,
ap.addrs_1,
ap.addrs_2,
ap.city,
ap.state,
ap.zip
FROM members as m
INNER JOIN address_primary AS ap ON
m.member_id = ap.members_id
WHERE m.active = 1
ORDER BY m.lname, m.fname”;[/php]

Thanks guys! I really appreciate the help. I was reading up on aliasing too, but was still having some issues. I’m away from my desk now for the weekend. I’ll take a fresh look at this stuff on Monday morning.

FROM members AS m
INNER JOIN address_primary AS ap ON m.member_id  = ap.members_id
INNER JOIN list_mailer AS lm ON m.member_id = lm.members_id
WHERE m.active = 1
AND lm.auction = 1

Excellent!. I ran what you had with the two tables and then added in the 3rd table INNER JOIN and its associated WHERE clause and didn’t get any errors and the returned data looks good.I’m off and running this morning! Thanks again.

Sponsor our Newsletter | Privacy Policy | Terms of Service