dongxian6715 2012-01-29 16:35
浏览 40
已采纳

php mysql连接多个表

I have two table

User

userID username     email
  1       abc    abc@abc.com
  2       def    def@def.com
  3       ghi    ghi@ghi.com

Referral

 refID userID      invEmail
   1      1      abc@def.com
   2      1      omg@mog.com
   3      1      def@def.com

So what i plan is to give a invited user 5 point meanwhile a inviter 10 point, so basically userID 1 gained 30 meanwhile userID 2 gained 5. The point i can do in PHP but one part i faced difficulty is when a invEmail to be identified. I don't mind separating into multiple queries if its will work.

How do i show this in sql?

I tried something like

SELECT *, count(r.userID) FROM user u, referral r WHERE u.userID = r.userID OR u.email = r.invEmail GROUP BY r.userID

It returned wrong value.

What i would like it to return, how much count is there inviter and invitee(matched email who has registered based on inviter invitation)

How should i do it?

Thank you.

Edit: i forgot to add something into question, what if i wanted the inviter to receive 10 points only if invitee registered? what i meant is that, only if invEmail exists in u.email then only userID received 10 point. Sorry for my mistake.

  • 写回答

1条回答 默认 最新

  • douze1332 2012-01-29 16:49
    关注

    You might be able to do something like this:

    select points.userId, points.username, points.email, sum(points.points)
    FROM
    (select u.*, count(*)*10 as points
      from user u
        join referral on u.userID = r.userID
        join user verify_user on r.invEmail = verify_user.email
      group by u.userID, u.username, u.email
    UNION
    select u.*, count(*)*5 as points
      from user u
        join referral on u.email = r.invEmail and u.userID != r.userID
      group by u.userID, u.username, u.email
    ) as points
    group by points.userId, points.username, points.email
    

    I think you need two separate selects to get the points for each type of registration, combined with a union statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。