duan89197 2015-09-06 08:46
浏览 62

php mysql三个左连接方法

I have three table:

Article:

|id|title|timestamp|......

group_tags:

|id|content_id|tags_id

tags_name:

|id|name|

I need to list article title with any tag id:

function _tags_search_($id,$type){
    $DB_QUERY = mySqli::f("SELECT title FROM " . ARTICLES . " LEFT JOIN " . POSTS_TAGS . " ON " . ARTICLES . ".id = " . POSTS_TAGS . ".content_id WHERE 
    " .POSTS_TAGS . ".tags_id = ? AND approved = 1 ORDER BY timestamp DESC LIMIT 12", $id);

        foreach($DB_QUERY as $row){
         $data[] = $row;
        }

    return $data;
}

this worked for me and show list of article title.

But I need to show tag name for tags id in addition to list title like this:

Search result for : Linux

I have two way :

  • three left join method ( if true how do?)

  • fetch another query for show tag name.

I think three join is better and faster. how do show tags name with three join method?!

  • 写回答

1条回答 默认 最新

  • douniewei6346 2015-09-08 15:33
    关注

    try this

    SELECT title, name from
    group_tags g
    INNER JOIN article ON a.id = g.content_id
    INNER JOIN tags_name t ON t.id = g.tags_id
    

    Let me know if you face any issue

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用