Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

sql - mysql syntax explanation

I would like to know what the a.*, c.name, ... a.access etc means. In other words, what exactly am I referring to when I add a letter before the dot and the funciton of the dot.

Here is an example of code where I found this occurrence:

$query = "SELECT a.*, c.name as categoryname,c.id as categoryid, ".
         "c.alias as categoryalias, c.params as categoryparams".
        " FROM #__k2_items as a".
        " LEFT JOIN #__k2_categories c ON c.id = a.catid";

        $query .= " WHERE a.published = 1"
        ." AND a.access <= {$aid}"
        ." AND a.trash = 0"
        ." AND c.published = 1"
        ." AND c.access <= {$aid}"
        ." AND c.trash = 0"
        ;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you look at the FROM clause, you see this:

FROM #__k2_items as a

And in the LEFT JOIN clause, you see this:

LEFT JOIN #__k2_categories c ON c.id = a.catid

That aliases the #__k2_items table (whatever it's really called) to the name a, and #__k2_categories to c, respectively. In this case it's just to save typing and improve the readability of the query, really.

The dot associates a column name to a table name so MySQL knows which tables to look in, in case you have columns of the same name in more than one table involved in the query. That is, it resolves column ambiguity.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...