Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do the “l” and “p” mean in this SQL statement?

I have the following statement which filters out a result from multiple tables. I am not quite understanding the p after tablename and the l after anothertablename. Are they generic or purposeful?

SELECT id, title, l.id, username FROM tablename p JOIN anothertablename l ON (l.id = p.id) WHERE p.id='15' LIMIT 1

like image 571
codacopia Avatar asked Oct 28 '25 10:10

codacopia


1 Answers

Those are called alias

It's just an abbreviation to use instead of always using the full table names.

Imagine if you had two tables called Products_Orders_items and Customers_orders_items if both had the column id, in a JOIN you would write:

SELECT * 
FROM Products_Orders_items
INNER JOIN Customers_orders_items 
    ON Customers_orders_items.id = Products_Orders_items.id
WHERE Customers_orders_items.otherField = 'bla';

Using alias you can make it easier to read:

SELECT * 
FROM Products_Orders_items a
INNER JOIN Customers_orders_items b ON a.id = b.id
WHERE b.otherField = 'bla';

There are also some cases where it is mandatory to use like when you use derived tables, such as:

SELECT * 
FROM (
    SELECT * 
    FROM Products_Orders_items a
    INNER JOIN Customers_orders_items b ON a.id = b.id
    WHERE b.otherField = 'bla'
) a

If you don't use an alias here, the query will give you an error.

like image 194
Filipe Silva Avatar answered Oct 30 '25 00:10

Filipe Silva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!