반응형
다른 테이블에서 ID가 있는 행 선택
이 테이블이라고 부르자terms_relation
:
+---------+----------+-------------+------------+------------+--+
| term_id | taxonomy | description | created_at | updated_at | |
+---------+----------+-------------+------------+------------+--+
| 1 | categ | non | 3434343434 | 34343433 | |
| 2 | categ | non | 3434343434 | 3434343434 | |
| 3 | tag | non | 3434343434 | 3434343434 | |
| 4 | tag | non | 3434343434 | 3434343434 | |
+---------+----------+-------------+------------+------------+--+
그리고 이것은 테이블이다.terms
:
+----+-------------+-------------+
| id | name | slug |
+----+-------------+-------------+
| 1 | hello | hello |
| 2 | how are you | how-are-you |
| 3 | tutorial | tutorial |
| 4 | the end | the-end |
+----+-------------+-------------+
테이블의 모든 행을 선택하려면terms
및 테이블terms_relation
표에서 분류법입니다.terms_relation
는categ
이 경우 2개의 쿼리가 필요합니까?아니면 이 쿼리가 필요합니까?join
스테이트먼트?
다음을 시도해 보십시오(서브쿼리).
SELECT * FROM terms WHERE id IN
(SELECT term_id FROM terms_relation WHERE taxonomy = "categ")
또는 다음과 같은 방법을 사용할 수 있습니다(JOIN).
SELECT t.* FROM terms AS t
INNER JOIN terms_relation AS tr
ON t.id = tr.term_id AND tr.taxonomy = "categ"
2개의 테이블에서 모든 필드를 수신하는 경우:
SELECT t.id, t.name, t.slug, tr.description, tr.created_at, tr.updated_at
FROM terms AS t
INNER JOIN terms_relation AS tr
ON t.id = tr.term_id AND tr.taxonomy = "categ"
서브쿼리를 사용할 수 있습니다.
SELECT *
FROM terms
WHERE id IN (SELECT term_id FROM terms_relation WHERE taxonomy='categ');
두 테이블의 모든 열을 표시해야 하는 경우:
SELECT t.*, tr.*
FROM terms t, terms_relation tr
WHERE t.id = tr.term_id
AND tr.taxonomy='categ'
SELECT terms.*
FROM terms JOIN terms_relation ON id=term_id
WHERE taxonomy='categ'
언급URL : https://stackoverflow.com/questions/10562915/selecting-rows-with-id-from-another-table
반응형
'programing' 카테고리의 다른 글
Vue Chart.js - 막대 그래프의 단순한 점/선 (0) | 2022.09.24 |
---|---|
Numpy 어레이를 이미지로 저장 (0) | 2022.09.24 |
MySQL 8.0 - 클라이언트가 서버에서 요청한 인증 프로토콜을 지원하지 않으므로 MySQL 클라이언트를 업그레이드하십시오. (0) | 2022.09.24 |
mariadb 행 수준 읽기 잠금 (0) | 2022.09.24 |
Maven 2에서 가능한 모든 목표를 나열하십시오. (0) | 2022.09.21 |