1

I want to use a CTE to use a subquery in two parts of the query. Unfortunately, MySQL doesn't have CTEs. Is there a way to do this without creating a temporary variable?

I don't have a specific query to simplify. I want to know the general technique. If you need a concrete example, here's one with CTE:

with subquery as (select * from t)
select *, (select count(*) from subquery c) from subquery a, subquery b 

What is the equivalent in MySQL?

2
  • Could you show the actual query you want to simplify? Commented Jul 20, 2013 at 18:57
  • 1
    @JoachimIsaksson I have read the other questions where people simplify for the particular situation without answering this part of the question. I want to reuse the query in a general sense. Thanks. Commented Jul 20, 2013 at 19:03

1 Answer 1

1

As far as I am aware, the closest equivalent in MySQL is by creating a view:

create view subquery as select * from t;

select *, (select count(*) from subquery c) from subquery a, subquery b;

SQLFiddle here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.