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)
I’m querying bid_tag
as follows:
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) …
So I need to grab the meta_value
of meta_key
“bid_name”, for example… amongst other values.
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…
I really appreciate it!
Join on both the
id
and themeta_key
to select your desired value. If you want values that go with other keys in the meta table as well, one way is to join multiple times:If the values won’t necessarily be present in the meta table, use a
LEFT OUTER JOIN
instead and it will returnnull
for whatever is missing.Here’s how your query would go:
there’re some ways to use join:
1)use
inner join
or equally justjoin
:The result will have only that records which
pet_id
exists inwp_postmeta
. The other will be omitted. And ifwp_postmeta
have a couple of records with the sameid
(and existspet_id
equal thisid
) then you’ll get a couple records with this pet_id (BUT with differentmeta_key
andmeta_value
).2)use
left join
:In this case behavior will be the same except one thing – even if there aren’t any
id
for apet_id
the result will contain record fromwp_postmeta
with thispet_id
. Butmeta_key
andmeta_value
in this case will benull
.