简体   繁体   English

MySQL内部联接查询给出三行

[英]Mysql inner join query gives a row three times

Thanks for reading. 谢谢阅读。

I'm trying to learn inner join and join. 我正在尝试学习内部联接和联接。 My aim is to comparing that if the user created the community is also marked as an admin in the CommunityMembers table (I'm not sure if INNER JOIN achieves this) and take all other community information from Community table related to CommunityID (this is possible with inner join as I understand). 我的目的是比较如果用户创建的社区在CommunityMembers表中也被标记为管理员(我不确定INNER JOIN是否实现了此目的),并从Community表中获取所有与CommunityID相关的其他社区信息(可能与我了解的内部联接)。

I have two tables called Community and CommunityMembers. 我有两个表,分别称为Community和CommunityMembers。

the structure is for Community(it has other data such as date, contents etc...): 该结构适用于社区(它具有其他数据,例如日期,内容等):

CommunityID -    Slug      - CreatedByUser
     1      -    video     -      2
     2      -    funny     -      2
     3      -    picture   -      4

for CommunityMembers 社区会员

 CmID - UserID  -      Slug         - Power
   1  -   2     -      video        - admin
   2  -   2     -      funny        - admin
   3  -   4     -      picture      - admin

my php code: ( $_SESSION['UserID'] is 2 ) 我的php代码:( $_SESSION['UserID']是2)

<?php

$sql = $dbc->prepare(" SELECT cm.*, com.* 
                       FROM Community com
                       INNER JOIN CommunityMembers cm ON cm.UserID = com.CreatedByUser
                       WHERE cm.UserID = '" . $_SESSION['UserID'] . "'");

$sql->execute();

if($sql->rowCount()){
    echo  $sql->rowCount();
    while($data = $sql->fetch()){
        $output .= $data['Slug'] .'<br />';
    }
    echo $output;
}else{
    $_ERROR = "no record";
}
?>

echo $output; prints 版画

video
video
video
funny
funny
funny

and echo $sql->rowcount(); echo $sql->rowcount(); prints 6 版画6

Could you please help with this? 您能帮忙吗? Why is this printing same thing 3 times? 为什么此打印相同的东西3次? and is my solution right to check if is user created the community is marked as admin in the CommunityMembers or do I need to check it? 并且我的解决方案对检查用户创建的社区是否在CommunityMembers中被标记为admin还是我需要进行检查的权利?

Thanks. 谢谢。

Try this 尝试这个

$sql = $dbc->prepare(" 
SELECT cm.*, com.* 
FROM Community com
INNER JOIN CommunityMembers cm ON cm.UserID = com.CreatedByUser
WHERE cm.UserID = '" . $_SESSION['UserID'] . "'

GROUP BY com.CreatedByUser
");
$sql = $dbc->prepare(" SELECT DISTINCT cm.*, com.* 
                   FROM Community com
                   INNER JOIN CommunityMembers cm ON cm.UserID = com.CreatedByUser
                   WHERE cm.UserID = '" . $_SESSION['UserID'] . "'");

SELECT DISTINCT 选择地区

http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html

If all you need to know if its admin, don't bring all fields from CommunityMembers. 如果您只需要了解其管理员,就不要带走CommunityMembers中的所有字段。 Try this 尝试这个

$sql = $dbc->prepare(" SELECT DISTINCT cm.Power, com.*
                       FROM Community com
                       LEFT JOIN CommunityMembers cm ON cm.UseriID=com.CreaterByUser
                       WHERE cm.UserID = '" . $_SESSION['UserID'] . "'");

Didn't test it, but this should work even without DISTINCT. 没有测试,但是即使没有DISTINCT,它也应该可以工作。

If you select the 2 tables result SELECT cm.*, com.* it will return all differents couples so try to select only specifics fields you need. 如果选择2个表结果SELECT cm.*, com.* ,它将返回所有不同的对,因此请尝试仅选择所需的特定字段。

PS : What is the full result, I mean not only the field slug ? PS:什么是完整的结果,我指的不仅是现场slug

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM