-2

Why would I use letters in front of each value in my query like this? In the database, each of these values is WITHOUT the letter in front.

    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)

I have tried looking it up, but I can't find it anywhere.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Gjert
  • 1,069
  • 1
  • 18
  • 48

3 Answers3

4

When in SQL you need to use more than one table for a query, you can do this:

SELECT person.name, vehicle.id FROM person, vehicle;

OR you can do it smaller, and put like this

SELECT p.name, v.id FROM person p, vehicle v;

It's only for reducing the query length, and it's useful for you

Dharman
  • 30,962
  • 25
  • 85
  • 135
Kyto
  • 181
  • 3
  • 12
3

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.
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
1

Consider you are accessing 2 tables and both have same column name say 'Id', In query you can easily identify those columns using letters like a.Id == d.Id if first table has alias name 'a' and second table 'b'. Or else It would be very difficult to identify which column belongs which table especially when you have common table columns.

Sagar
  • 642
  • 3
  • 14