Hi, I received a long bit of code from a PHP programmer that I am trying to get my head around. I’d like to understand what, for example, SELECT m.url, sc.name, sc.title FROM menu m means. Why is it not just SELECT url, name, title FROM menu. What is .sc and m?
SELECT m.url, sc.name, sc.title
FROM menu m
INNER JOIN static_content sc
ON m.name = sc.menu_name
WHERE m.name = "' . $pageid . '"
ORDER BY order_no ASC;
I see there is an inner join in the sql statement. So m or sc are just the aliases of respective tables to denote the table name. Instead of using those aliases you can use the table name itself too.
SELECT menu.url, static_content.name, static_content.title
FROM menu
INNER JOIN static_content
ON menu.name = static_content.menu_name
WHERE menu.name = "' . $pageid . '"
ORDER BY order_no ASC;
the reason you use them is twofold, and both apply when the query contains more than one table
if the tables share a column name, an alias is required when you use the column name, otherwise the database won’t know which one you want and you’ll get a syntax error
identifying the table that a column belongs to even if the column name is unique among all the tables in the query is best practice because it (self-)documents the query