dqjcb132285 2014-12-11 19:48
浏览 37
已采纳

如何摆脱这个双mysql连接中的重复?

I looked through other Q&A's and I didn't see my scenario.

My duplicates are weird. Different logged in managers would get different amount of duplicates depending on how many users are under them.

SELECT user_groups.*, user_table.*, my_quotes.* 
FROM user_groups 
RIGHT JOIN user_table 
ON user_table.usergroup = user_groups.groupid 
RIGHT JOIN my_quotes 
ON my_quotes.groupid = user_groups.groupid

Need to display results from my_quotes, but while also displaying other info from other tables.

A quote belongs to a user and also to the manager. when a manager creates a quote for a user, but there happens to be other users that the manager manages, the quotes table from the query above will show a quote for every user even though the quote belongs to just one user.

So say there are 2 users under the manager. user 1 has a quote and user 2 has a quote. the quotes mysql table will only have 2 records so the html table should only display 2 records, but instead it displays 4 records. 2 for quote 1 and 2 for quote 2.

Now let's say another user is added under the manager. the html table will then display 3 records for each quote. it's weird.

The following doesn't work:

SELECT DISTINCT user_groups.*, user_table.*, my_quotes.* 
FROM user_groups 
RIGHT JOIN user_table 
ON user_table.usergroup = user_groups.groupid 
RIGHT JOIN my_quotes 
ON my_quotes.groupid = user_groups.groupid

user_groups:

groupid, name, userid

user_table:

userid, username, email, usergroup, name, company, address, phone, manager, client

my_quotes:

id, userid, groupid, clientname, ponumber, date, postage, qty, price, total, approved

couple of the tables have too many fields to post. i just put the relevant ones to this problem.

one of the answers given helped, but there's still something wrong. as you can see, there are 3 different queries depending if logged in user is admin, manager, or client. the manager and client one displays correct records, and the admin one almost does, but there is a missing record for admin.

if($_SESSION['usergroup']==1) {

    $tableresult = mysql_query('

        SELECT user_groups.*, user_table.*, my_quotes.* 
        FROM user_groups 
        RIGHT JOIN user_table 
        ON user_table.usergroup = user_groups.groupid 
        RIGHT JOIN my_quotes 
        ON my_quotes.userid = user_table.userid

    ') or die(mysql_error());

} else if($manager==1) {

    $tableresult = mysql_query('

        SELECT user_groups.*, user_table.*, my_quotes.* 
        FROM user_groups 
        RIGHT JOIN user_table 
        ON user_table.usergroup = user_groups.groupid 
        RIGHT JOIN my_quotes 
        ON my_quotes.userid = user_table.userid 
        WHERE user_groups.userid='.$_SESSION['userid'].'

    ') or die(mysql_error());

} else {

    $tableresult = mysql_query('

        SELECT my_quotes.*, user_table.* 
        FROM my_quotes 
        INNER JOIN user_table 
        ON my_quotes.userid = user_table.userid 
        WHERE user_table.userid = '.$_SESSION['userid'].' 
        AND (approved = 1 OR approved = 2)

    ') or die(mysql_error());

}
  • 写回答

2条回答 默认 最新

  • douchu4048 2014-12-11 19:55
    关注

    According To you example quotes are related with user's id, then you should joinby user's id not by the group.

    SELECT user_groups.*, user_table.*, my_quotes.* 
    FROM user_groups 
    RIGHT JOIN user_table 
    ON user_table.usergroup = user_groups.groupid 
    RIGHT JOIN my_quotes 
    ON my_quotes.userid = user_table.userid
    

    New Answer For Admin: (Admin can see all the quotes)

    SELECT user_table.*, my_quotes.* 
    FROM user_table
    RIGHT JOIN my_quotes 
    ON my_quotes.userid = user_table.userid //this will grab all the quotes
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区
  • ¥15 在grasshopper里DrawViewportWires更改预览后,禁用电池仍然显示
  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译