Mysql - Help me alter this search query to get desired results

前端 未结 1 413
别跟我提以往
别跟我提以往 2020-12-04 04:35

Following is a dump of the tables and data needed to answer understand the system:-

The system consists of tutors and classes. The data in the table

相关标签:
1条回答
  • 2020-12-04 05:06

    I think this query would help you:

    SET @tag1 = 1, @tag2 = 4; -- Setting some user variables to see where the ids go. (you can put the values in the query)
    
    SELECT wtagrels.id_wc,
      SUM(DISTINCT( wtagrels.id_tag =@tag1 OR wtagrels.id_tutor =@tag1)) AS key_1_total_matches,
      SUM(DISTINCT( wtagrels.id_tag =@tag2 OR wtagrels.id_tutor =@tag2)) AS key_2_total_matches
    FROM   all_tag_relations AS wtagrels
    WHERE  ( wtagrels.id_tag =@tag1 OR wtagrels.id_tag =@tag2 )
    GROUP  BY wtagrels.id_wc
    HAVING key_1_total_matches = 1 AND key_2_total_matches = 1
    LIMIT  0, 20
    

    It returns id_wc = 1.
    For (6, 3) the query returns nothing.

    0 讨论(0)
提交回复
热议问题