<bdo id='uB1De'></bdo><ul id='uB1De'></ul>

  1. <i id='uB1De'><tr id='uB1De'><dt id='uB1De'><q id='uB1De'><span id='uB1De'><b id='uB1De'><form id='uB1De'><ins id='uB1De'></ins><ul id='uB1De'></ul><sub id='uB1De'></sub></form><legend id='uB1De'></legend><bdo id='uB1De'><pre id='uB1De'><center id='uB1De'></center></pre></bdo></b><th id='uB1De'></th></span></q></dt></tr></i><div id='uB1De'><tfoot id='uB1De'></tfoot><dl id='uB1De'><fieldset id='uB1De'></fieldset></dl></div>

    <small id='uB1De'></small><noframes id='uB1De'>

      <legend id='uB1De'><style id='uB1De'><dir id='uB1De'><q id='uB1De'></q></dir></style></legend>
      <tfoot id='uB1De'></tfoot>

      Mysql - 帮助我更改此搜索查询以获得所需的结果

      时间:2023-10-09

              <tbody id='bd30C'></tbody>
            • <tfoot id='bd30C'></tfoot>
                <bdo id='bd30C'></bdo><ul id='bd30C'></ul>

                <small id='bd30C'></small><noframes id='bd30C'>

                <legend id='bd30C'><style id='bd30C'><dir id='bd30C'><q id='bd30C'></q></dir></style></legend>
              • <i id='bd30C'><tr id='bd30C'><dt id='bd30C'><q id='bd30C'><span id='bd30C'><b id='bd30C'><form id='bd30C'><ins id='bd30C'></ins><ul id='bd30C'></ul><sub id='bd30C'></sub></form><legend id='bd30C'></legend><bdo id='bd30C'><pre id='bd30C'><center id='bd30C'></center></pre></bdo></b><th id='bd30C'></th></span></q></dt></tr></i><div id='bd30C'><tfoot id='bd30C'></tfoot><dl id='bd30C'><fieldset id='bd30C'></fieldset></dl></div>
                本文介绍了Mysql - 帮助我更改此搜索查询以获得所需的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                以下是回答理解系统所需的表格和数据的转储:-

                该系统由导师和班级组成.All_Tag_Relations 表中的数据存储了每个注册的导师和一个导师创建的每个班级的标签关系.标签关系用于搜索类.

                The system consists of tutors and classes. The data in the table All_Tag_Relations stores tag relations for each tutor registered and each class created by a tutor. The tag relations are used for searching classes.

                CREATE TABLE IF NOT EXISTS `Tags` (
                  `id_tag` int(10) unsigned NOT NULL auto_increment,
                  `tag` varchar(255) default NULL,
                  PRIMARY KEY  (`id_tag`),
                  UNIQUE KEY `tag` (`tag`),
                  KEY `id_tag` (`id_tag`),
                  KEY `tag_2` (`tag`),
                  KEY `tag_3` (`tag`),
                  KEY `tag_4` (`tag`)
                ) ENGINE=InnoDB  DEFAULT CHARSET=latin1;
                
                INSERT INTO `Tags` (`id_tag`, `tag`) VALUES
                (1, 'Sandeepan'),
                (2, 'Nath'),
                (3, 'first'),
                (4, 'class'),
                (5, 'new'),
                (6, 'Bob'),
                (7, 'Cratchit');
                
                
                CREATE TABLE IF NOT EXISTS `All_Tag_Relations` (
                  `id_tag` int(10) unsigned NOT NULL default '0',
                  `id_tutor` int(10) default NULL,
                  `id_wc` int(10) unsigned default NULL,
                  KEY `All_Tag_Relations_FKIndex1` (`id_tag`),
                  KEY `id_wc` (`id_wc`),
                  KEY `id_tag` (`id_tag`)
                ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
                
                
                INSERT INTO `All_Tag_Relations` (`id_tag`, `id_tutor`, `id_wc`) VALUES
                (1, 1, NULL),
                (2, 1, NULL),
                (3, 1, 1),
                (4, 1, 1),
                (6, 2, NULL),
                (7, 2, NULL),
                (5, 2, 2),
                (4, 2, 2),
                (8, 1, 3),
                (9, 1, 3);
                

                以下是我的查询:-

                此查询在标签"表中搜索first class"(first = 3 和 class = 4 的标签)并返回所有这些类,以便在类名称中同时出现词 first 和 class.

                This query searches for "first class" (tag for first = 3 and for class = 4, in Tags table) and returns all those classes such that both the terms first and class are present in the class name.

                SELECT wtagrels.id_wc,SUM(DISTINCT( wtagrels.id_tag =3)) AS
                       key_1_total_matches,
                       SUM(DISTINCT( wtagrels.id_tag =4))                AS
                       key_2_total_matches
                FROM   all_tag_relations AS wtagrels
                WHERE  ( wtagrels.id_tag =3
                          OR wtagrels.id_tag =4 )
                GROUP  BY wtagrels.id_wc
                HAVING key_1_total_matches = 1
                       AND key_2_total_matches = 1
                LIMIT  0, 20  
                

                它返回 id_wc = 1 的类.

                And it returns the class with id_wc = 1.

                但是,我希望搜索显示所有这些课程,以便所有搜索词都出现在课程名称或其导师名称中因此,搜索Sandeepan class"(wtagrels.id_tag = 1,4)或Sandeepan Nath"也会返回 id_wc=1 的类.和搜索.搜索Bob First"不应返回任何类.

                But, I want the search to show all those classes such that all the search terms are present in the class name or its tutor name So that searching "Sandeepan class" (wtagrels.id_tag = 1,4) or "Sandeepan Nath" also returns the class with id_wc=1. And Searching. Searching "Bob First" should not return any classes.

                请修改上述查询或建议新查询,如果可能,请使用 MyIsam - 全文搜索,但以某种方式帮助我获得结果.

                Please modify the above query or suggest a new query, if possible using MyIsam - fulltext search, but somehow help me get the result.

                推荐答案

                我认为这个查询会对您有所帮助:

                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
                

                它返回id_wc = 1.
                对于 (6, 3) 查询不返回任何内容.

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

                这篇关于Mysql - 帮助我更改此搜索查询以获得所需的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:根据条件连接两个表以及第二个表中的记录数 下一篇:什么时候用LEFT JOIN,什么时候用INNER JOIN?

                相关文章

              • <small id='AFnQY'></small><noframes id='AFnQY'>

                  • <bdo id='AFnQY'></bdo><ul id='AFnQY'></ul>
                  <legend id='AFnQY'><style id='AFnQY'><dir id='AFnQY'><q id='AFnQY'></q></dir></style></legend>
                    <tfoot id='AFnQY'></tfoot>
                    <i id='AFnQY'><tr id='AFnQY'><dt id='AFnQY'><q id='AFnQY'><span id='AFnQY'><b id='AFnQY'><form id='AFnQY'><ins id='AFnQY'></ins><ul id='AFnQY'></ul><sub id='AFnQY'></sub></form><legend id='AFnQY'></legend><bdo id='AFnQY'><pre id='AFnQY'><center id='AFnQY'></center></pre></bdo></b><th id='AFnQY'></th></span></q></dt></tr></i><div id='AFnQY'><tfoot id='AFnQY'></tfoot><dl id='AFnQY'><fieldset id='AFnQY'></fieldset></dl></div>