dongwujie7477 2013-03-23 18:12
浏览 31
已采纳

选择内部联接的id

I'm trying to construct an user profile, so I'm showing all her likes from the database. But I want to look if the user that have the active session has liked some of the user profile likes.

So, the table name is loves and the structure is:

photo_id (int)
nick     (varchar)
date     (timestamp)

photos table structure:

photo_id (int)
path     (varchar)
title    (varchar)
category (varchar)
nick     (varchar)
date     (timestamp)

This is how I'm traying to do the query:

SELECT photos.photo_id
FROM photos
INNER JOIN loves ON loves.nick = 'userProfileName'
WHERE loves.nick =  'userWithActiveSession'
AND photos.photo_id = loves.photo_id
ORDER BY loves.photo_id DESC 
LIMIT 100

This query should return all photo ID's that the user with active session have liked with the liked photos from the profile requested user.

EXAMPLE

loves table:

nick         photo_id
userProfile  26  
userProfile  1000  
userProfile  27
userProfile  520
userSession  26  
userSession  680  
userSession  1000

So the query should return only two photos_id (1000 and 26), because both users has liked the same photo_id.

Is there any way to modify this code to do what I want?

  • 写回答

3条回答 默认 最新

  • douweng7308 2013-03-23 18:21
    关注

    So you want all the photos owned by X (photos.nick = X) and liked by Y?

    SELECT photos.photo_id FROM photos INNER JOIN loves
    ON loves.photo_id = photos.photo_id 
    WHERE loves.nick = Y AND photos.nick = X
    ORDER BY photos.photo_id DESC LIMIT 100
    

    If you want photos liked by both X and Y then you need to join loves to itself, matching the photo_ids from the two copies of the table to each other, and conditioning that one table's nick matches X and the other's matches Y. (See comments)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥150 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装