Jump to content

[SOLVED] Help joining please


Brian W

Recommended Posts

I'm trying to figure this join stuff and am a little lost.

I have two tables;

Stores: id | name | details | location

Location: id | name | details

Every store is in a location but not every location has a store.

I want to get a list of all the stores with their location's name also so I can list em using PHP.

This is what i have for my SQL so far

<?php
$sql = "SELECT *
FROM `Stores`
LEFT JOIN `Locations` 
ON `Stores`.`Location`=`Locations`.`ID`
ORDER BY `Stores`.`Location`";
?>

From what I've seen of other people's code, I need to do something with some syntax along the lines of "AS"... but haven't really found something like this yet while hunting my favorite tutorial site.

Any help, input, whatnot appreciated. thanks

Link to comment
https://forums.phpfreaks.com/topic/135854-solved-help-joining-please/
Share on other sites

nvm i guess. figures it out once I figured out what I should look for. "Alias" is what I should have been searching for to begin with... After trial and error (E_ALL!!!) i got it working.

$sql = "SELECT 
Stores.Name AS Name, Stores.Location AS LID, Stores.ID AS ID, Locations.Name AS Location
FROM Stores
LEFT JOIN Locations 
ON Stores.Location=Locations.ID
ORDER BY Location";

Thanks anyways guys

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.