[英]SQL Joining another table based on the ID of the initial table
I really just can't seem to grasb the sytanx of SQL Joins, etc ... so I'm needing some help with this (which I think is quite simple) 我真的真的似乎无法掌握SQL Joins的共生关系,等等...所以我需要一些帮助(我认为这很简单)
I'm querying bid_tag
as follows: 我正在查询
bid_tag
,如下所示:
SELECT paid_date, term, pet_id FROM bid_tag WHERE active = 1
And I need to use the pet_id
to then grab some information from another table wp_postmeta
, where the table is actually in a meta_key
meta_value
structure (WordPress) ... 我需要使用
pet_id
从另一个表wp_postmeta
获取一些信息,该表实际上位于meta_key
meta_value
结构(WordPress)中。
So I need to grab the meta_value
of meta_key
"bid_name", for example... amongst other values. 因此,我需要获取
meta_value
“ meta_key
”的meta_value,例如...以及其他值。
TABLE
id | meta_key | meta_value
1 bid_name Max
That ID
is the same ID
that I need to connect the initial table to... 这
ID
是一样的ID
,我需要初始表连接到...
I really appreciate it! 我真的很感激!
Join on both the id
and the meta_key
to select your desired value. 加入
id
和meta_key
来选择所需的值。 If you want values that go with other keys in the meta table as well, one way is to join multiple times: 如果您还希望值与元表中的其他键一起使用,则一种方法是多次联接:
SELECT bt.paid_date, bt.term, bt.pet_id,
bn.meta_value as bidname,
bo.meta_value as bidother
FROM bid_tag bt
INNER JOIN wp_postmeta bn on bn.id = bt.pet_id and bn.meta_key = 'bid_name'
INNER JOIN wp_postmeta bo on bo.id = bt.pet_id and bo.meta_key = 'bid_other'
WHERE bt.active = 1
If the values won't necessarily be present in the meta table, use a LEFT OUTER JOIN
instead and it will return null
for whatever is missing. 如果值不一定在元表中出现,请改用
LEFT OUTER JOIN
,如果缺少任何内容,它将返回null
。
Here's how your query would go: 这是查询的方式:
SELECT B.meta_key, B.meta_value, A.pet_id
FROM bid_tag A
JOIN wp_postmeta B
ON B.id = A.pet_id;
there're some ways to use join: 有一些使用联接的方法:
1)use inner join
or equally just join
: 1)使用
inner join
或相等地只是join
:
select
--some columns
from bid_tag
join wp_postmeta on
bid_tag.pet_id = wp_postmeta.id
where
wp_postmeta.meta_key = 'bid_name'
The result will have only that records which pet_id
exists in wp_postmeta
. 其结果将只具有哪些记录
pet_id
存在于wp_postmeta
。 The other will be omitted. 另一个将被省略。 And if
wp_postmeta
have a couple of records with the same id
(and exists pet_id
equal this id
) then you'll get a couple records with this pet_id ( BUT with different meta_key
and meta_value
). 如果
wp_postmeta
具有相同几个记录id
(和存在pet_id
等于这个id
),那么你会得到这个pet_id一对夫妇记录( 但具有不同的meta_key
和meta_value
)。
2)use left join
: 2)使用
left join
:
select
--some columns
from bid_tag
left join wp_postmeta on
bid_tag.pet_id = wp_postmeta.id
where
wp_postmeta.meta_key = 'bid_name'
In this case behavior will be the same except one thing - even if there aren't any id
for a pet_id
the result will contain record from wp_postmeta
with this pet_id
. 在这种情况下,除了一件事外,行为将是相同的-即使
pet_id
没有任何id
,结果也将包含来自wp_postmeta
带有此pet_id
记录。 But meta_key
and meta_value
in this case will be null
. 但是在这种情况下,
meta_key
和meta_value
将为null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.