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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应