简体   繁体   English

我有一个 sql 查询错误

[英]i have an sql query Error

Error is : Every derived table must have its own alias is there any error?错误是:每个派生表都必须有自己的别名 有什么错误吗?

$sql = "SELECT r_name FROM(
                          SELECT b.r_name,a.check_in,a.check_out
                          FROM rooms b, bookings a
                          WHERE b.r_name = a.r_name )
                   WHERE(
                          check_in <= CAST($mychickIn AS DATETIME) AND check_out >= CAST($mychickIn AS DATETIME)
                          OR check_in < CAST($mycheckOut AS DATETIME) AND check_out >= CAST($mycheckOut AS DATETIME)
                          OR check_in >= CAST($mychickIn AS DATETIME) AND check_out <= CAST($mycheckOut AS DATETIME))
  ";

You have to give the table, which you have created with the SELECT in the FROM case, a alias.您必须为在 FROM 情况下使用 SELECT 创建的表提供别名。 To access a "table" created in the FROM, it needs a name.要访问在 FROM 中创建的“表”,它需要一个名称。

Try something like this:尝试这样的事情:

$sql = "SELECT t.r_name FROM(
                      SELECT b.r_name,a.check_in,a.check_out
                      FROM rooms b, bookings a
                      WHERE b.r_name = a.r_name ) t
               WHERE(
                      t.check_in <= CAST($mychickIn AS DATETIME) AND t.check_out >= CAST($mychickIn AS DATETIME)
                      OR t.check_in < CAST($mycheckOut AS DATETIME) AND t.check_out >= CAST($mycheckOut AS DATETIME)
                      OR t.check_in >= CAST($mychickIn AS DATETIME) AND t.check_out <= CAST($mycheckOut AS DATETIME))";

It is nested selection statement, and inner select results are put in temporary table.它是嵌套选择语句,内部选择结果放在临时表中。 You should name temporary table explicitly.您应该明确命名临时表。

select * from (select * from t) should be changed to 
select * from (select * from t) as t_tmp;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM