developer tip

MySQL "WITH"절

copycodes 2020. 9. 12. 10:11
반응형

MySQL "WITH"절


MySQL을 사용하여 "WITH"절이있는 뷰를 만들려고합니다.

WITH authorRating(aname, rating) AS
   SELECT aname, AVG(quantity)
   FROM book
   GROUP BY aname

그러나 MySQL이 이것을 지원하는 것 같지 않습니다.

나는 이것이 꽤 표준이라고 생각했고 오라클이 이것을 지원한다고 확신합니다. 어쨌든 MySQL이 "WITH"절을 사용하도록 강제하는 것이 있습니까? MyISAM 및 innoDB 엔진으로 시도했습니다. 둘 다 작동하지 않습니다.


업데이트 : MySQL 8.0은 마침내 재귀 CTE를 포함한 공통 테이블 표현식의 기능을 얻습니다.

다음은이를 알리는 블로그입니다. http://mysqlserverteam.com/mysql-8-0-labs-recursive-common-table-expressions-in-mysql-ctes/

아래는 내가 2008 년에 처음 썼던 초기 답변입니다.


MySQL은 Common Table ExpressionsWITH 라고도하는 SQL-99에 정의 된 구문을 사용하는 쿼리를 지원하지 않습니다 .

이것은 2006 년 1 월부터 MySQL에 대한 기능 요청입니다. http://bugs.mysql.com/bug.php?id=16244

공통 테이블 표현식을 지원하는 기타 RDBMS 제품 :

WITH 절을 지원하지 않는 기타 데이터베이스 (2014 년 2 월 기준) :


다음과 같은 무언가에 관심이있을 수 있습니다.

select * from (
    select * from table
) as Subquery

올바른 구문이 있습니다.

WITH AuthorRating(AuthorName, AuthorRating) AS
   SELECT aname         AS AuthorName,
          AVG(quantity) AS AuthorRating
   FROM Book
   GROUP By Book.aname

그러나 다른 사람들이 언급했듯이 MySQL은이 명령을 지원하지 않습니다. WITH는 SQL : 1999에 추가되었습니다. SQL 표준의 최신 버전은 SQL : 2008입니다. Wikipedia 에서 SQL : 1999의 다양한 기능을 지원하는 데이터베이스에 대한 추가 정보를 찾을 수 있습니다 .

MySQL은 전통적으로 SQL 표준에 대한 지원이 다소 뒤처졌지만 Oracle, SQL Server (최근) 및 DB2와 같은 상용 데이터베이스는이 표준을 좀 더 밀접하게 따랐습니다. PostgreSQL은 일반적으로 꽤 표준을 준수합니다.

MySQL의 로드맵을 살펴볼 수 있습니다. 이 기능이 언제 지원되는지는 확실하지 않지만 읽을 수있는 롤업 쿼리를 만드는 데는 좋습니다.


오라클은 WITH를 지원합니다.

이렇게 보일 것입니다.

WITH emps as (SELECT * FROM Employees)
SELECT * FROM emps WHERE ID < 20
UNION ALL
SELECT * FROM emps where Sex = 'F'

@ysth WITH is hard to google because it's a common word typically excluded from searches.

You'd want to look at the SELECT docs to see how subquery factoring works.

I know this doesn't answer the OP but I'm cleaning up any confusion ysth may have started.


Building on the answer from @Mosty Mostacho, here's how you might do something equivalent in MySQL,for a specific case of determining what entries don't exist in a table, and are not in any other database.

select col1 from (
   select 'value1' as col1 union
   select 'value2' as col1 union
   select 'value3' as col1
) as subquery
left join mytable as mytable.mycol = col1
where mytable.mycol is null
order by col1

You may want to use a text editor with macro capabilities to convert a list of values to the quoted select union clause.


MariaDB is now supporting WITH. MySQL for now is not. https://mariadb.com/kb/en/mariadb/with/


Have you ever tried Temporary Table? This solved my convern:

create temporary table abc (
column1 varchar(255)
column2 decimal
);
insert into abc
select ...
or otherwise
insert into abc
values ('text', 5.5), ('text2', 0815.8);

Then you can use this table in every select in this session:

select * from abc inner join users on ...;

   WITH authorRating as (select aname, rating from book)
   SELECT aname, AVG(quantity)
   FROM authorRating
   GROUP BY aname

참고URL : https://stackoverflow.com/questions/324935/mysql-with-clause

반응형