简体   繁体   English

MYSQL联接无法正常工作

[英]MYSQL joins not working as expected

I am trying to create a join statement to solve my problem but cannot get my head around it. 我正在尝试创建一个连接语句来解决我的问题,但无法解决。

I am a new to join statements so please bear with me if my sql statement is nonsense. 我是新加入联接的语句,所以如果我的sql语句是胡说八道,请多多包涵。

I have two tables, one is a table of questions where users have asked questions about items for sale. 我有两个表,一个是问题表,用户在其中询问了有关待售物品的问题。

Second is a table of items that the user has asked a question about. 第二是用户提出问题的项目表。

Table one called questions consists of question_ref, questioner_user_ref, item_ref, seller_ref, question_text, timestamp 表一称为questionsquestion_ref, questioner_user_ref, item_ref, seller_ref, question_text, timestamp

questions
===========
+--------------+--------------------+---------+-----------+--------------+----------+
| question_ref |questioner_user_ref |item_ref |seller_ref |question_text |timestamp |
+--------------+--------------------+---------+-----------+--------------+----------+

Table two called my_item_comments consists of questioner_ref, item_ref, last_question_ref 表二称为my_item_commentsquestioner_ref, item_ref, last_question_ref

my_item_comments
===========
+---------------+---------+------------------+
|questioner_ref |item_ref |last_question_ref |
+---------------+---------+------------------+

I have set up table two to keep track of items that the user has asked questions about so they can be informed when someone else asks the seller a question or the seller answers. 我已经建立了表2,以跟踪用户提出的问题,以便在其他人向卖方提出问题或卖方回答时得到通知。

So I want to create a recordset of questions that a). 因此,我想创建一个关于a)问题的记录集。 Someone has answered a question about an item the user is selling b). 有人回答了有关用户出售的商品的问题b)。 A seller has replied to a question the user asked, 卖家回答了用户提出的问题,
c). C)。 A third user has asked a question about an item that the user has also asked a question about. 第三用户已经问到有关该商品的问题,该用户也已经问了有关该商品的问题。

A bit like facebook's commenting system, where you are informed about comments people have made on statuses that you have commented on. 有点像facebook的评论系统,在该系统中,您可以获悉人们对您发表评论的状态发表的评论。

So my current sql statement is as follows 所以我当前的sql语句如下

$user_ref= current logged in user


  $sql=mysql_query("
    SELECT * FROM questions
    LEFT JOIN my_item_comments
    ON questions.item_ref=my_item_comments.item_ref
    WHERE questions.questioner_user_ref!='$user_ref'
    AND (questions.seller_ref='$user_ref' OR questions.item_ref=my_item_comments.item_ref
    ORDER BY timestamp DESC");

The results don't work and I think its because of the OR questions.item_ref=my_item_comments.item_ref but for the life of me I cannot work it out. 结果不起作用,由于OR questions.item_ref=my_item_comments.item_ref ,我认为是这样OR questions.item_ref=my_item_comments.item_ref但对我而言,我OR questions.item_ref=my_item_comments.item_ref

Any help would be greatly appreciated, even if it means restructuring my database with more tables or new fields in the tables. 任何帮助将不胜感激,即使这意味着用更多表或表中的新字段来重构数据库。

thanks in advance, Barry 预先感谢,巴里

SELECT * FROM questions
LEFT JOIN my_item_comments ON questions.item_ref=my_item_comments.item_ref
WHERE 
   questions.questioner_user_ref != '$user_ref' AND 
   (questions.seller_ref='$user_ref' OR questions.item_ref=my_item_comments.item_ref)
ORDER BY timestamp DESC

I think you were missing a ) to enclose the OR clause (in the example above I added it) 我认为您缺少)来包含OR子句(在上面的示例中,我添加了它)

You should try to improve readability for yourself and us so that it is easier to debug and easier for us to help you :-) 您应该尝试提高自己和我们的可读性,以便更容易调试和更容易为您提供帮助:-)

$q = "
    SELECT * 
      FROM questions
RIGHT JOIN my_item_comments ON questions.item_ref = my_item_comments.item_ref
     WHERE questions.questioner_user_ref != '".$user_ref."'
       AND (    questions.seller_ref = '".$user_ref."' 
            OR  questions.item_ref=my_item_comments.item_ref
            )
  ORDER BY timestamp DESC";

$rs = mysql_query($q);

And preferably just the query with dummy data 并且最好只是带有伪数据的查询

    SELECT * 
      FROM questions
RIGHT JOIN my_item_comments ON questions.item_ref = my_item_comments.item_ref
     WHERE questions.questioner_user_ref != 'dummy_ref'
       AND (    questions.seller_ref = 'dummy_ref' 
            OR  questions.item_ref=my_item_comments.item_ref
            )
  ORDER BY timestamp DESC

Try a right join for your query. 尝试right join查询。 Left join will indeed get all the rows. 左联接确实将获得所有行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM