我如何在这个SQL语句中使用“as”?

时间:2014-01-21 07:00:51

标签: mysql sql sql-server phpmyadmin

这里

SELECT 
  COUNT(*) 
FROM
  tbl_department 
  RIGHT JOIN tbl_video 
    ON tbl_video.department_id = tbl_department.department_id 
  LEFT JOIN tbl_language 
    ON tbl_language.language_id = tbl_video.video_language 
WHERE tbl_video.department_id LIKE '%1%' 

我已经尝试了

SELECT 
  COUNT(*) 
FROM
  tbl_department 
  RIGHT JOIN tbl_video 
    ON tbl_video.department_id = tbl_department.department_id 
  LEFT JOIN tbl_language 
    ON tbl_language.language_id = tbl_video.video_language AS numbers 
WHERE tbl_video.department_id LIKE '%1%' 

但它不起作用。 帮忙。

5 个答案:

答案 0 :(得分:2)

用于选择

SELECT 
  COUNT(*)  AS numbers
.....

不要在某处使用AS ..在选择

中使用它

答案 1 :(得分:2)

 SELECT 
 COUNT(*) as total_records
 FROM
 tbl_department 
 RIGHT JOIN tbl_video 
 ON tbl_video.department_id = tbl_department.department_id 
 LEFT JOIN tbl_language 
 ON tbl_language.language_id = tbl_video.video_language 
 WHERE tbl_video.department_id LIKE '%1%';

这将显示所有选定记录的计数。

答案 2 :(得分:1)

你在找这个吗?

SELECT COUNT(*) AS Numbers FROM ...
                ^^^^^^^^^^

答案 3 :(得分:0)

使用(*)时它表示所有列,“AS”可用于一列,因此表tbl_department中的列具有一列。

答案 4 :(得分:0)

基本上你也可以为别名提供表名,以便在join子句中使用, 像这样的东西:

SELECT 
COUNT(*) as total_records
FROM
tbl_department as td --the word 'as' is not mandatory , you can just directly give it an alias
RIGHT JOIN tbl_video as tv
ON tv.department_id = td.department_id -- using the aliases
LEFT JOIN tbl_language as tl -- using the aliases
ON tl.language_id = tv.video_language -- using the aliases
WHERE tv.department_id LIKE '%1%';
顺便说一句,最佳做法是使用“左连接” 所以你可以安排你加入的桌子的顺序

参见示例:

SELECT 
COUNT(*) as total_records
FROM tbl_video as tv
LEFT JOIN  tbl_department as td 
ON tv.department_id = td.department_id 
LEFT JOIN tbl_language as tl 
ON tl.language_id = tv.video_language 
WHERE tv.department_id LIKE '%1%';