Stack Stack - 4 months ago 16
MySQL Question

Is there any default table to I use it as an alias?

I have this query:

->prepare("INSERT INTO
events (post_id, table_code, other_id, user_id, author_id, date_time )
select ?, 15, ?, ?, ?, UNIX_TIMESTAMP()
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) ));

Please focus on this line:

from (select 1) as a

My query works as well and all fine.

I just remember I read somewhere there is a default-table-name (something like
which I can use it instead of
(select 1) as a
in this case. Anybody knows what's that?


You are thinking of dual. This is borrowed from Oracle. So instead of:

select ?, 15, ?, ?, ?, UNIX_TIMESTAMP()
from (select 1) as a

You can write:

select ?, 15, ?, ?, ?, UNIX_TIMESTAMP()
from dual

This is documented 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.

I'm not sure this is true, though. I think from may be needed with where and group by clauses.