PDA

View Full Version : better way to do this in one go?


ewel
12-11-2008, 11:55 PM
Sometimes I need to do something with MySQL and then I look around for examples and get by. Like this I made the below bit of code. But I am wondering if there is a better way to do this? Now I have two queries and it would be nicer if it were possible to do it in one go. Unfortunately I don't know enough to know where to look for answers myself..

Here is what I have:
$query = "SELECT 'locid' FROM #__eventlist_events WHERE 'id'=".$id;
$database->setQuery($query);
$venueid = $database->loadResult();
$query = "SELECT 'street','plz','city','state','country' FROM #__eventlist_venues WHERE 'id'=".$venueid;
$database->setQuery($query);
$el_address = $database->loadResult();


Thanks in advance for your suggestions!

Fumigator
12-12-2008, 12:41 AM
Yeah this looks like a simple JOIN.


SELECT e.locid, v.street, v.plz, v.city, v.state, v.country
FROM #__eventlist_events as e
JOIN #__eventlist_venues as v
ON e.locid = v.id
WHERE e.id= $id

ewel
12-12-2008, 08:29 AM
Thanks! I'll try that and I'll read up about joins..

Would you be able to tell me or point me to information about how to use single and double quotation marks? Sometimes I see the columns or table name in 'single' or "double" quotes and I wonder when to use those or not.

Fumigator
12-12-2008, 04:34 PM
In normal MySQL syntax, single-quotes or double-quotes around column names will throw a syntax error. Your database class must parse them out or something, because single or double quotes tells MySQL the text is a string literal, not a column name.

MySQL gives you the option of using the backtick (`) to enclose column names, allowing you to use reserved words as column names. Personally I hate this practice. It clutters up the code, it doesn't work in other DBMSs and it's a lazy naming standard. But that's just my opinion :p