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
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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.