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 제품 :
- Oracle 9i 릴리스 2 이상 :
http://www.oracle-base.com/articles/misc/with-clause.php - Microsoft SQL Server 2005 이상 :
http://msdn.microsoft.com/en-us/library/ms190766(v=sql.90).aspx - IBM DB2 UDB 8 이상 :
http://publib.boulder.ibm.com/infocenter/db2luw/v8/index.jsp?topic=/com.ibm.db2.udb.doc/admin/r0000879.htm - PostgreSQL 8.4 이상 :
https://www.postgresql.org/docs/current/static/queries-with.html - Sybase 11 이상 :
http://dcx.sybase.com/1100/en/dbusage_en11/commontblexpr-s-5414852.html - SQLite 3.8.3 이상 :
http://sqlite.org/lang_with.html - HSQLDB :
http://hsqldb.org/doc/guide/dataaccess-chapt.html#dac_with_clause - Firebird 2.1 이상 (재귀 쿼리를 지원하는 최초의 오픈 소스 DBMS) : http://www.firebirdsql.org/file/documentation/release_notes/html/rlsnotes210.html#rnfb210-cte
- H2 데이터베이스 (그러나 에만 재귀) :
http://www.h2database.com/html/advanced.html#recursive_queries
WITH 절을 지원하지 않는 기타 데이터베이스 (2014 년 2 월 기준) :
- Informix (Informix
CONNECT BY
는 한때 Oracle에서 사용 했던 구문을 지원하지만 )
http://pic.dhe.ibm.com/infocenter/idshelp/v115/index.jsp?topic=%2Fcom.ibm.sqls.doc%2Fids_sqs_2033. htm
다음과 같은 무언가에 관심이있을 수 있습니다.
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
'developer tip' 카테고리의 다른 글
constexpr은 인라인을 의미합니까? (0) | 2020.09.12 |
---|---|
http 호스트 헤더 란 무엇입니까? (0) | 2020.09.12 |
Java에서 Linux 쉘 명령을 호출하는 방법 (0) | 2020.09.12 |
Java 8에서 ArrayList의 기본 용량이 이제 0 인 이유는 무엇입니까? (0) | 2020.09.12 |
NHibernate의 역 속성 (0) | 2020.09.12 |