dqroktbn005028 2016-01-15 21:39
浏览 10

来自MySQLi查询的不同值

I am trying to only show unique userIds (userIds are (0,1,2,3,4,5,6,7,8,9 etc...) for the query I am running. I tried using DISTINCT in my query, but it only shows me unique values of the rows that have 2 or more of the same userId.

Is there a way I can use php to only show the unique values. My weak points are arrays and it makes it more complicated because its using data from a MySQLi query.

Example right now I have with the query now (lets say its GROUP BY rentPaid DESC and the rent total is 800.00 for all users):

  userID  rentPaid  rentMonth
  2--------800.00------April  
  1--------500.00------April  
  3--------400.00------April  
  3--------400.00------April  
  1--------200.00------April  
  1--------100.00------April  

Example desired output:

  userID  rentPaid  rentMonth 
  2--------800.00------April  
  1--------500.00------April  
  3--------400.00------April  

Can I do this with MYSQL because I tried DISTINCT and it wouldn't work, how about PHP?

Query:

        SELECT      
            properties.*,
            leases.*,
            users.userId, users.primaryPhone,
            CONCAT(users.userFirstName,' ',users.userLastName) AS user,
            admins.adminName, payments.*
        FROM
            properties
            LEFT JOIN leases ON properties.propertyId = leases.propertyId
            LEFT JOIN assigned ON properties.propertyId = assigned.propertyId
            LEFT JOIN admins ON assigned.adminId = admins.adminId
            LEFT JOIN users ON properties.propertyId = users.propertyId
            LEFT JOIN payments ON properties.propertyId = payments.propertyId
        WHERE
            payments.rentMonth = '$currentMonth' AND
            payments.rentYear = '$currentYear'

Edit: Please excuse my formatting, this is my first post.

Edit: Added query....its long, but works lol. I only want unique userIds (no double or triple userIds etc...)

  • 写回答

1条回答 默认 最新

  • douhao7677 2016-01-15 21:49
    关注

    I suspect this is what you want:

    SELECT userID, MAX(rentPaid) AS maxRentPaid, rentMonth
    FROM yourTable
    WHERE rentMonth = "April"
    GROUP BY userID
    ORDER BY maxRentPaid
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化