简体   繁体   English

如何在另一个表中执行的mysql查询中添加条件?

[英]how can i add condition in mysql query executed in another table?

I develope a website for E-books, i have in database table for authors and table for publishers .. sometimes the author name is added also in publishers table as a publisher Now i have his name as an author and a publisher .. when i search in the site for his name, it return twice because i search in authors table and in publishers table then merge two queries 我开发了一个电子书网站,我在作者数据库表和出版商表中都有..有时作者名称也以出版商的形式添加在出版商表中。现在,我的名字是作者和出版商..当我在网站上搜索他的名字,它返回两次,因为我在authors表和Publishers表中搜索然后合并两个查询

this is my code :- 这是我的代码:-

function generate_results($keyword, $row = 0) {


        $result1 = $this->db->query("SELECT au_id,au_name,au_state,SUBSTR(au_info,1,190) AS au_info,au_img FROM d_author where (au_name LIKE '%$keyword%' or au_info LIKE '%$keyword%') and au_state = '1' limit $row,20");


        $result2 = $this->db->query("SELECT pub_id,pub_name,pub_state,SUBSTR(pub_info,1,190) AS pub_info,pub_img FROM d_publishing where (pub_name LIKE '%$keyword%' or pub_info LIKE '%$keyword%') and and pub_state = '1' limit $row,20");


        $results = array_merge($result1->result_array(), $result2->result_array());

        return $results;
    }  

Now i want to modify the second query to something like that : select all publishers from "publishers table" where the name of publisher is like $keyword and this $keyword doesn't exist in authors table .. I mean if this name exist in authors don't select it in publishers How can i translate that meaning to Mysql Query 现在,我想将第二个查询修改为类似的内容:从“发布者表”中选择所有发布者,其中发布者的名称类似于$ keyword,而该$ keyword在authors表中不存在..我的意思是如果该名称存在于作者没有在发布者中选择它。如何将其翻译成Mysql Query?

First, check out sql-injections before continuing to develop in your ebook-application. 首先,在继续在您的电子书应用程序中进行开发之前,请先检查sql-injections。 Looks like your keyword is not checked to be a safe parameter. 看来您的关键字没有被检查为安全参数。 And just to be sure, do you know about csrf and xss? 而且可以肯定的是,您了解csrf和xss吗? If not, check about that too. 如果没有,也检查一下。 This is very important. 这个非常重要。

Secondaly, you should consider working on your database design to avoid having duplicated values . 其次,您应该考虑进行数据库设计,以避免值重复 Check out "database normalization" for more information. 请查看“数据库规范化”以获取更多信息。 Seems like you could do another table to extract your "contact information" like name, state, id etc. This would make it possible for your author-table and publisher-table to use a "contact_id" referencing the contact-information-table. 似乎您可以做另一个表来提取您的“联系信息”,例如名称,状态,ID等。这将使您的作者表和发布者表可以使用引用联系信息表的“ contact_id”。

Last but not least, to answer your question, you can generelly solve such problems with an "anti join". 最后但并非最不重要的一点是,要回答您的问题,您可以使用“反连接”来彻底解决此类问题。 Use a left join on the authors table in the second query and check for "IS NULL" on matches with the publisher table. 在第二个查询中的authors表上使用左联接,并检查与Publisher表的匹配项是否为“ IS NULL”。 More information here: http://explainextended.com/2009/09/18/not-in-vs-not-exists-vs-left-join-is-null-mysql/ 此处提供更多信息: http : //explainextended.com/2009/09/18/not-in-vs-not-exists-vs-left-join-is-null-mysql/

I do not know if I understand your database design completly right, but it also seems like an UNION combined with a DISTINCT could help you - and you wouldn't even need this array_merge -stuff. 我不知道我是否完全了解您的数据库设计,但是似乎UNIONDISTINCT结合可以为您提供帮助-并且您甚至不需要此array_merge -stuff。 I would suggest you to check out these two commands in the mysql docs. 我建议您在mysql文档中检查这两个命令。

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

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