【问题标题】:How to retrieve the status of users has same interest Mysql query issue如何检索具有相同兴趣的用户的状态 Mysql 查询问题
【发布时间】:2016-11-12 06:10:56
【问题描述】:

我正在学习 MYsql 和 php。现在在mysql中遇到了这个奇怪的问题。我有两张桌子 soc_users_interestsoc_status。请看下表结构。

----------                -----------
soc_status                soc_users_interest
----------                ------------
status_ID                 soc_interest_id (Auto increment)
status                    userID
userID                    Interest

我正在尝试列出与用户(使用该应用程序的用户)具有相同兴趣的用户的所有状态。我进行了查询,但它没有返回正确的结果。这是我写的不起作用的查询。

 SELECT soc_status.statusID, soc_status.status FROM soc_status
WHERE soc_status.userID =

      (SELECT  
  soc_users_interest.userID as firstUser , 
  soc_users_interest.interest as firstUserInterest , 

  secondUser.userID as secondUser, 
  secondUser.interest as secondUserInterest 

 FROM soc_users_interest JOIN
  soc_users_interest secondUser 
     ON soc_users_interest.interest = soc_users_interest.interest) WHERE soc_users_interests.userID = "23445";

I am getting this error -> `[2016-07-09 20:53:26] [21000][1241] Operand should contain 1 column(s)`

如何列出与用户(使用应用程序的用户)具有相同兴趣的用户的所有状态?这个问题的任何解决方案?感谢您的帮助;)

【问题讨论】:

  • “它不起作用”根本没有给我们任何有用的信息。如果它工作正常,你就不会在这里问问题。什么是预期的输出?电流输出是多少?有错误信息吗?
  • 我更新了问题。 @乔斯林
  • 试试soc_status.userID in( )而不是soc_status.userID =
  • 相同的错误操作数应包含 1 列 @starshine531
  • 好的,发生这种情况的原因是您在子查询中选择了 4 个不同的字段。它只需是此查询当前结构的一种方式。

标签: php mysql sql sql-server sql-server-2008


【解决方案1】:
SELECT soc2.userID, soc2.status, int.interest  FROM soc_status soc1
join soc_status soc2 on soc1.user_id=soc2.user_id and soc1.user_id="23445"
join soc_users_interest int on soc1.user_id=int.user_id

【讨论】:

  • [2016-07-10 12:25:39] [42S22][1054] 未知列
  • 这是什么?这是你得到的错误吗?尝试在您的问题/cmets 中更清楚?
【解决方案2】:

只是一个问题。为什么需要 2 张桌子。将这些数据合二为一。如果您觉得不能有 1 个表,请使用具有正确列数的表 JOIN WHERE tbl1.Userid = tbl2.Userid

【讨论】:

    【解决方案3】:

    这样的东西应该可以满足您的需求。虽然我没有你的桌子,所以我没有测试过。

    SELECT  
      soc_users_interest.userID as firstUser , 
      soc_users_interest.interest as firstUserInterest , 
      secondUser.userID as secondUser, 
      secondUser.interest as secondUserInterest ,
      soc_status.statusID, soc_status.status
    
     FROM soc_users_interest, soc_users_interest secondUser,soc_status
     WHERE  
     firstUser.interest = secondUser.interest
     AND soc_status.userID=secondUser.userID
     AND firstUser.userID = "23445";
    

    【讨论】:

    • 检查更新的问题。我修改了你所说的查询。
    • 你能说得更具体点吗?什么没用?您是否收到错误消息,或者只是与您想要的结果不同?
    • imgur.com/a/GCkAc @starshine531 。它显示了不同的结果
    • 您想在结果中得到什么?也许我误解了你想要什么。您只需要状态消息吗?如果是这样,只需从 select 子句中删除不需要的项目你能给出一个理想结果的示例吗?
    • 我想要与用户 a 有相同兴趣的用户的状态。 @starshine531
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-21
    • 1970-01-01
    • 2018-07-14
    相关资源
    最近更新 更多