Php 如何使用公共字段连接mysql中的两个表
我有两个mysql表。即Php 如何使用公共字段连接mysql中的两个表,php,mysql,Php,Mysql,我有两个mysql表。即 德布卢邮政 喜欢 //德布卢邮政 id || name || username || unique_key || pub 1 Jit jit11 unkey1 demo 2 Rah rah11 unkey2 demo1 3 dee dee11 unkey3 demo2 //喜欢
id || name || username || unique_key || pub
1 Jit jit11 unkey1 demo
2 Rah rah11 unkey2 demo1
3 dee dee11 unkey3 demo2
//喜欢
id || post_id || unique_key
1 2 unkey3
我的问题是,如何根据表db_post中的unique_key字段混合这两个表
//输出应如下所示。(其中唯一的_key='unkey3')
db\u post
中的id
字段和db\u like
中的post\u id
字段应该匹配。使用左键连接
select dp.id,
dp.name,
dp.unique_key,
dp.pub,
dl.post_id
from db_post dp
left join db_like dl on dp.unique_key = dl.unique_key
order by dp.id desc;
在unique\u键上使用左连接
,如
select dp.id,
dp.name,
dp.unique_key,
dp.pub,
dl.post_id
from db_post dp
left join db_like dl on dp.unique_key = dl.unique_key
order by dp.id desc;
在unique\u键上使用左连接
,如
select dp.id,
dp.name,
dp.unique_key,
dp.pub,
dl.post_id
from db_post dp
left join db_like dl on dp.unique_key = dl.unique_key
order by dp.id desc;
在unique\u键上使用左连接
,如
select dp.id,
dp.name,
dp.unique_key,
dp.pub,
dl.post_id
from db_post dp
left join db_like dl on dp.unique_key = dl.unique_key
order by dp.id desc;
这需要左连接,连接条件为db_unique_key=db_like.unique_key和db_like.unique_key='unkey3')
这需要左连接,连接条件为db_unique_key=db_like.unique_key和db_like.unique_key='unkey3')
这需要左连接,连接条件为db_unique_key=db_like.unique_key和db_like.unique_key='unkey3')
这需要左连接,连接条件为db_unique_key=db_like.unique_key和db_like.unique_key='unkey3')
根据你的例子,post_id应该是2。@Abhis:-哦,对不起,我错了。请再次查看编辑后的问题。根据您的示例,post_id应该是2。@Abhis:-哦,对不起,我的错误。请再次查看编辑后的问题。根据您的示例,post_id应该是2。@Abhis:-哦,对不起,我的错误。请再次查看编辑后的问题。根据您的示例,post_id应该是2。@Abhis:-哦,对不起,我的错误。再次查看已编辑的问题。重复相同的结果。@JithinVarghese,根据您的编辑,您需要按ID字段排序。如果有帮助,请参阅答案中的编辑。重复相同的结果。@JithinVarghese,根据您的编辑,您需要按ID字段排序。如果有帮助,请参阅答案中的编辑。重复相同的结果。@JithinVarghese,根据您的编辑,您需要按ID字段排序。如果有帮助,请参阅答案中的编辑。重复相同的结果。@JithinVarghese,根据您的编辑,您需要按ID字段排序。如果有帮助,请参见回答中的编辑。在这种情况下,只需将连接条件更改为左连接db_like l on l.post_id=p.id和l.unique_key='unkey3'
id
字段中的db_post
和db_like
中的字段应匹配。在这种情况下,只需将连接条件更改为左连接l.post上的db_like l=p.id和l.unique_key='unkey3'
db_post中的db_like l
字段和db_like
中的字段应该匹配。在这种情况下,只需将连接条件更改为左连接l.post上的db_like l=p.id和l.unique_key='unkey3'
iddb_post
和db_-like
中的post-id
字段应该匹配。在这种情况下,只需将连接条件更改为l上的左连接db-like l。post-id=p.id和l.unique_-key='unkey3'
中的id
字段和db-like中的post-id
字段应该匹配。