So i have a left join query but both of my tables have some of the same column names. I am wondering how to specifically call one of the columns. My query looks like this:
SELECT * FROM Contacts AS Contacts LEFT JOIN Events AS Events ON Contacts.PK =
Events.Realted_Contact
So these tables have columns that are the same. For example, both tables have a PK column for their primary key. Right now when i echo $row['PK'] it only returns the Events PK and not the Contacts PK. Im trying to do something like this but it's not working:
echo $row['Contacts.PK'];
But when I do this it's echoing nothing. How can i echo a column that has the same name as another column in the join. Thank you for any help.
Use AS
in the SELECT
statament of the query
SELECT Contacs.*, Events.PK AS EventsPK FROM Contacts AS Contacts LEFT JOIN Events AS Events ON Contacts.PK = Events.Realted_Contact
And then access the value like this
echo $row['EventsPK'];
Be ware that you are using LEFT JOIN
and the Event's columns could be NULL
给列起这样的别名:
SELECT Contacts.PK as Contacts_PK...
Query result you can check by using print_r function. Example as print_r(query result);
SELECT Contacts.PK as contact_pk FROM Contacts AS Contacts LEFT JOIN Events AS Events ON Contacts.PK = Events.Realted_Contact
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.