我有这个查询:
$db
->prepare("INSERT INTO
events (post_id, table_code, other_id, user_id, author_id, date_time )
select ?, 15, ?, ?, ?, UNIX_TIMESTAMP()
UNION ALL
select ?, 15, ?, ?, ?, UNIX_TIMESTAMP()
from (select 1) as a
where 1=?
")->execute(array($answer_id, $answer_id, $author_ques_id, $author_ques_id,
$answer_id, $answer_id, $author_ques_id, $author_ans_id,
($yourCondition?1:0) ));
请关注这一行:
from (select 1) as a
我的查询也有效,一切都很好。
我只记得我在某处读到有一个默认表名称(类似于double
),我可以使用它来代替(选择1)在本例中为
。有人知道那是什么吗?
最佳答案
您正在考虑双重
。这是从 Oracle 借来的。所以代替:
select ?, 15, ?, ?, ?, UNIX_TIMESTAMP()
from (select 1) as a
你可以写:
select ?, 15, ?, ?, ?, UNIX_TIMESTAMP()
from dual
这已记录在案here :
DUAL is purely for the convenience of people who require that all SELECT statements should have FROM and possibly other clauses. MySQL may ignore the clauses. MySQL does not require FROM DUAL if no tables are referenced.
不过,我不确定这是真的。我认为 from
可能需要与 where
和 group by
子句一起使用。
关于mysql - 是否有任何默认表可供我将其用作别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37847374/