问题
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"
;
回答1:
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.
回答2:
a.Something
means the column Something
in the table a
. In your statement, you created aliases on your tables:
FROM #__k2_items as a
LEFT JOIN #__k2_categories c
#__k2_items
becoming aliased to a
, and #__k2_categories
becoming aliased to c
.
So SELECT a.*, c.name as categoryname,c.id as categoryid, c.alias as categoryalias, c.params as categoryparams
will select all fields from #__k2_items
, and the fields id
, alias
and params
from #__k2_categories
.
Hope that helps.
回答3:
They are table names. So you are also matching table names: a is all tables that have a name matching #__k2_items
回答4:
The word before the dot is the table name, the word after it is the column name. In this example, a
is converted into #__k2_items
, c
into #__k2_categories
. This is used for shorter and more readable SQL.
回答5:
You're aliasing your tables/views. "FROM #__k2_items as a" means the #__k2_items will be known as a "a" alias, so you won't have to use long table names.
This:
SELECT a.*, c.name as categoryname,c.id as categoryid, ...
means the same thing as this:
SELECT #__k2_items.*, #__k2_categories.name as categoryname, #__k2_categories.id as categoryid, ...
回答6:
- a.*: select all fields from the table a, which is actually the tables '#__k2_items' having a different alias specified by the 'as' keyword.
- c.name and the other similar expressions: select the field 'name' from the table 'c', which is again the table #__k2_categories.
- c.alias as categoryalias and the other similar expressions: renaming (aliasing) the field 'alias' in c.alias to 'categoryalias'.
Basically, as others already said:
- anything before the dot is the table name and anything after the table name is the field name.
- The 'as' statement as used to change the name. This is useful to have a useful name as the output of the SQL query. For example, you may be selecting two averages from two tables, one of them is PhD student average and the other is MSc student average, so you can change the name of the fields to make it easier to understand which average is from which table.
- Sometimes to make the syntax simpler, especially when you are selecting fields from many tables, you rename a table as you did for 'a' and 'c'.
回答7:
How to insert a record into the table # __k2_items:
$db = &JFactory::getDBO();
$query = "INSERT INTO jos_k2_items (`title`, `alias`, `catid`, `published`,
`introtext`, `fulltext`, `video`, `gallery`, `extra_fields`,
`extra_fields_search`, `created`, `created_by`, `created_by_alias`,
`checked_out`, `checked_out_time`, `modified`, `modified_by`,
`publish_up`, `publish_down`, `trash`, `access`, `ordering`, `featured`,
`featured_ordering`, `image_caption`, `image_credits`, `video_caption`,
`video_credits`, `hits`, `params`, `metadesc`, `metadata`, `metakey`,
`plugins`)
VALUES
('".$title."', '".$title."', ".$catid.", 0, '<p>".$introtext."</p>',
'', NULL, NULL, '".$extra_fields."', 'extra_fields_search', now(), 62,
'', 0, '0000-00-00 00:00:00', '', 62, '', '0000-00-00 00:00:00', 0, 0,
5, 0, 0, '', '', '', '', 0, '', '', 'robots=\nauthor=', '', '')
";
$db->setQuery($query);
$db->query();
来源:https://stackoverflow.com/questions/4586262/mysql-syntax-explanation