UPDATE
MySQL 8.0 introduced support for Common Table Expressions (CTE) i.e. 'WITH' which earlier versions of MySQL do not support.
https://dev.mysql.com/doc/refman/8.0/en/with.html
EDIT
Yes, we can assign an alias to an inline view (or, derived table, in the MySQL venacular) and reference that alias elsewhere in the query. In this example, my_query
is an alias we assign to the inline view.
SELECT my_query.val
FROM ( SELECT val FROM foo ) my_query
WHERE my_query.val
ORDER
BY my_query.val
But we cannot reference that alias in the FROM clause of a subquery. For example, in this construct:
SELECT my_query.val
FROM ( SELECT val FROM foo ) my_query
WHERE 7 IN ( SELECT val FROM my_query )
ORDER
BY my_query.val
the syntax is actually valid. But the reference to my_query
in the FROM
clause of the IN (subquery)
is not and can not be a reference to the table alias in the outer query. That's not allowed.
So we'd expect the query to probably throw an error about my_query
reference in the subquery being unresolved identifier "unknown table", something of that ilk.
Note that We could create a table named my_query
, e.g.
CREATE TABLE my_query (val INT PRIMARY KEY);
INSERT INTO my_query (val) VALUES (7);
And then re-run the query, and that reference my_query
would now resolve to the table. (Not the alias assigned in the query.)
The Common Table Expression (CTE) introduced a significant step forward in resolving this type of issue, the ability to reference an inline view in multiple places where it isn't otherwise allowed.
If the question is, "How to emulate Common Table Expression (CTE) functionality in MySQL", a google search leads to this question on SO:
How do you use the "WITH" clause in MySQL?
original
Did you mean CTE (Common Table Expression i.e. the WITH clause) rather than CMT ?
If you meant CMT, I apologize, because it's not clear what that acronym represents, in the context of this question. Maybe it means Centrally Managed Table, but I don't see how that relates.
If you are asking if Common Table Expression (CTE) is supported in MySQL, the answer to that is no. Or, at least, not yet.
From the SQL examples posted, there doesn't appear to be any need for the CTEs or for inline views, or even subqueries. Perhaps the SQL has been over-simplified to the point that it's not clear what the query is supposed to achieve. (It's not supposed to get a symmetric difference, so we aren't going to suggest the obvious alternatives, how to do that in MySQL.
It is valid to use a subquery (a SELECT) with IN
and NOT IN
. These are both valid:
foo IN ( subquery )
foo NOT IN ( subquery )
One big "gotcha" with NOT IN (subquery)
is when a NULL value is returned by the subquery. With a NULL value in the list, the NOT IN
evaluates to NULL, which means it can't evaluate to TRUE for any row. As a demonstration, consider:
SELECT 3 NOT IN (2,NULL,4)
Maybe that answers the question that's being asked.
We're kind of just guessing at what question is being asked. (Was there a question asked?)
What is meant by "DOES NOT WORK" ? The query is returning unexpected results? MySQL is returning an error? The query executes a long time and appears to "hang"? We're just guessing what DOES NOT WORK means.
Why are we using the old-school comma operator syntax for the join operation, rather than the (can we even still call it? newer) JOIN
keyword?