[英]how to do a join using mysql
I'm trying to create a page where there is content on top and at the bottom there is a section that if you click on the heading it will slide down to reveal some more content. 我正在尝试创建一个页面,该页面的顶部是内容,底部是一个部分,如果您单击标题,它将向下滑动以显示更多内容。 I'm having a slight problem on the bottom section. 我在底部有一个小问题。 When I try to do a join statement nothing shows up. 当我尝试执行连接语句时,什么都没有显示。
I have 3 tables. 我有3张桌子。 For the content table the id is the primary key. 对于内容表,id是主键。 For the menu table the id is the primary key. 对于菜单表,id是主键。 For the content_menu both the id_menu and id_content is the primary key. 对于content_menu,id_menu和id_content都是主键。
content:
id | title | content
1 | content1 | This is content 1
2 | content2 | This is content 2
menu:
id | title |active
1 | menu 1 | 1
2 | menu 2 | 1
content_menu:
id_menu | id_content
1 | 1
This is the code that I'm using 这是我正在使用的代码
$exQuery = "SELECT * FROM menu M, content C LEFT JOIN content_menu CxM ON M.`id`=CxM.`id_menu` C.`id`=CxM.`id_content` WHERE M.`active`='1'";
$query = mysql_query($exQuery);
while( $result = mysql_fetch_object($query) ){
$str.='<div class="additional-navigation-wrapper">';
$str.= '<div class="additional-navigation">';
$str.= '<a class="border-bottom-white padding-level-one inactive additional-nav-info" href="javascript:void(0);">';
$str.= $result->title;
$str.= '<img class="nav-arrow no-action floatright" src="images/nav-arrow-white.png" />';
$str.= '<span class="clearboth"></span>';
$str.= '</a>';
$str.= '<div class="additional-nav-info-wrapper">';
$str.= '<div class="additional-nav-info-inner" style="display:none;">';
$str.= '<h2>'.$result->title.'</h2>';
$str.= '<p>'.$page->content.'</p>';
$str.= '</div>';
$str.= '</div>';
$str.= '</div>';
$str.='</div>';
$i++;
}
return $str;
If you need any more information or if its unclear please let me know 如果您需要更多信息或不清楚,请通知我
SELECT * FROM menu M, content C LEFT JOIN content_menu CxM ON M.`id`=CxM.`id_menu`
C.`id`=CxM.`id_content` WHERE M.`active`='1'
In addition to the missing AND
between the two join terms that @user3591614 mentioned, you also have a problem when you mix comma-style join syntax with JOIN
syntax. 除了@ user3591614提到的两个联接项之间缺少AND
之外,在将逗号样式JOIN
语法与JOIN
语法混合使用时,您还会遇到问题。
The problem is that JOIN
has higher precedence than comma. 问题是JOIN
优先级高于逗号。 So at the time the query is evaluating the term M.id = CxM.id_menu
, it only knows about tables C
and CxM
. 因此,在查询评估术语M.id = CxM.id_menu
,它仅了解表C
和CxM
。 Therefore the table M
cannot be referenced by the join condition. 因此,连接条件不能引用表M
The solution is to stop using comma-style joins, and use modern syntax for joins consistently. 解决方案是停止使用逗号样式的连接,并始终使用现代语法进行连接。 Here's how I would write that query: 这是我编写该查询的方式:
SELECT * FROM menu M
LEFT JOIN content CxM ON M.id = CxM.id_menu
LEFT JOIN content C ON C.id = CxM.id_content
WHERE M.active=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.