By "letters in front", I assume you mean the qualifiers on the columns c.
, uu.
and so on. They indicate the table where the column comes from. In a sense, they are part of the definition of the column.
This is your query:
SELECT c.client_id, c.client_name, c.contactperson, c.internal_comment,
IF NULL(r.region, 'Alle byer') as region, c.phone, c.email,
uu.fullname as changed_by,
(select count(p.project_id)
from projects p
where p.client_id = c.client_id and (p.is_deleted != 1 or p.is_deleted is null)
) as numProjects
FROM clients c LEFT JOIN
users uu
ON c.db_changed_by = uu.id LEFT JOIN
regions r
ON c.region_id = r.region_id
WHERE (c.is_deleted != 1 or c.is_deleted is null)
In some cases, these are needed. Consider the on
clause:
ON c.region_id = r.region_id
If you leave them out, you have:
ON region_id = region_id
The SQL compiler cannot interpret this, because it does not know where region_id
comes from. Is it from clients
or regions
? If you used this in the select
, you would have the same issue -- and it makes a difference because of the left join
. This is also true in the correlated subquery.
In general, it is good practice to qualify column names for several reasons:
- The query is unambiguous.
- You (and others) readily know where columns are coming from.
- If you modify the query and add a new table/subquery, you don't have to worry about naming conflicts.
- If the underlying tables are modified to have new column names that are shared with other tables, then the query will still compile.