Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用公共字段连接mysql中的两个表_Php_Mysql - Fatal编程技术网

Php 如何使用公共字段连接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 //喜欢

我有两个mysql表。即

  • 德布卢邮政
  • 喜欢
  • //德布卢邮政

    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'
    id
    db_post
    db_-like
    中的
    post-id
    字段应该匹配。在这种情况下,只需将连接条件更改为
    l上的左连接db-like l。post-id=p.id和l.unique_-key='unkey3'
    中的
    id
    字段和
    db-like中的
    post-id
    字段应该匹配。