开发者

MySQL Conditional Join Queries (WordPress DB)

开发者 https://www.devze.com 2023-02-10 03:45 出处:网络
So I\'ve been looking through how to get this to work and haven\'t been able to find it, or really even properly search.

So I've been looking through how to get this to work and haven't been able to find it, or really even properly search.

I'm looking to join t1 to t2 where I get ALL results from t1, and it's only joined where t2's column value is "something", the开发者_Python百科 table is set up that there are many items in the t2 column. So if t2's column doesn't equal "something", then just have the other data (but mainly I only want ONE of each value for t1 row, because there will be a max of one t2 row that qualifies per t1 row).

select * t1 LEFT JOIN t2 where t2.column="something" AND t1 conditions.

Any help would be appreciated, the tables are WordPress tables, and the thing I'm asked to do would be easier done without WordPress knowing.

-- Actual code attempt:

$ SELECT * FROM posts LEFT JOIN postmeta ON post_id = id WHERE post_status='publish' AND post_type='portfolio' AND meta_key='rjmeta_video'


$Table 1
$ID     Title ....
$----------------------
$5      Some post I need outside of WP

$Table 2
$meta_id  post_id   meta_key  ....
$--------------------------------
$3        5         rjmeta_video
$4        5         _edit_lock
$5        5         _edit_last


I believe this is what you mean:

select distinct t1.* from t1 
LEFT JOIN t2 on [condition for join] and t2.column="something" 
where [t1 conditions].

This assumes that you are only interested in the t1 fields.

What we don't know:

  • table schemas
    • how are they joined? t2.t1_id = t1.id ?
  • t1 conditions

DISTINCT used above will eliminate duplicate rows if there are multiple matches for the join

EDIT (OP comment response):

SELECT distinct posts.* FROM posts 
LEFT JOIN postmeta ON (post_id = id and meta_key='rjmeta_video')
WHERE post_status='publish' AND post_type='portfolio'

This assumes that 'rjmeta_video' is the "something" you were referring to originally

0

精彩评论

暂无评论...
验证码 换一张
取 消