https://www.w3schools.com/sql/func_mysql_find_in_set.asp
MySQL FIND_IN_SET() Function
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
예제
문자열 배열 내의 "q"를 찾기
SELECT FIND_IN_SET("q", "s,q,l");
정의와 용례
FIND_IN_SET() 함수는 문자열 배열에서 문자열의 위치를 반환합니다.
구문
파라미터 값들
파라미터 설명string | 필수, 찾을 문자열 |
string_list | 필수, 콤마로 구분된 문자열들의 배열, 검색 대상 |
반환 값들
- 만약 문자열을 문자열 배열에서 찾지 못하면 0을 반환
- 만약 문자열이나 문자열 배열이 NULL이면 NULL을 반환
- 만약 문자열 배열이 공백문자열("") 이면 0을 반환
기술 상세
Works in:MySQL 4.0 부터 동작 |
How to create a MySQL hierarchical recursive query?
I have a MySQL table which is as follows: id name parent_id 19 category1 0 20 category2 19 21 category3 20 22 category4 21 ... ... ... Now, I want to have a single MySQL query to which I sim...
stackoverflow.com
WITH 문이 MySQL 버전 8부터 사용가능하기 때문에, 메뉴 트리 구조를 만들기 위한 WITH RECUERSIVE 문을 사용할 수 없다. 이때 사용할 수 있는 쿼리를 찾다가 다음 쿼리를 찾았다.
MySQL 8+
with recursive cte (id, name, parent_id) as (
select id,
name,
parent_id
from products
where parent_id = 19
union all
select p.id,
p.name,
p.parent_id
from products p
inner join cte
on p.parent_id = cte.id
)
select * from cte;
MySQL 5.x
select id,
name,
parent_id
from (select * from products
order by parent_id, id) products_sorted,
(select @pv := '19') initialisation
where find_in_set(parent_id, @pv)
and length(@pv := concat(@pv, ',', id))
select p6.parent_id as parent6_id,
p5.parent_id as parent5_id,
p4.parent_id as parent4_id,
p3.parent_id as parent3_id,
p2.parent_id as parent2_id,
p1.parent_id as parent_id,
p1.id as product_id,
p1.name
from products p1
left join products p2 on p2.id = p1.parent_id
left join products p3 on p3.id = p2.parent_id
left join products p4 on p4.id = p3.parent_id
left join products p5 on p5.id = p4.parent_id
left join products p6 on p6.id = p5.parent_id
where 19 in (p1.parent_id,
p2.parent_id,
p3.parent_id,
p4.parent_id,
p5.parent_id,
p6.parent_id)
order by 1, 2, 3, 4, 5, 6, 7;
이런식으로 적절히 바꿔 쓸 수 있다.
오라클에서 이런식의 구문을 살펴보려면 여기를 참조
https://leedohyun1985.tistory.com/99
Hierarchical Queries 계층 구조 쿼리들
https://docs.oracle.com/en/database/oracle/oracle-database/21/sqlrf/Hierarchical-Queries.html SQL Language Reference docs.oracle.com https://docs.oracle.com/cd/B19306_01/server.102/b14200/queries003..
leedohyun1985.tistory.com