简体   繁体   English

MySQL:结合两个重复的查询

[英]MySQL: Combining two repetitive queries

I have this in info in a table where sometime the pd_id is NULL 我在表格中有这个信息,有时pd_idNULL

doc_id  doc_title   doc_order   pg_id 
14      a.zip       1           NULL    
15      b.zip       2           NULL    
12      c.zip       1           16  
13      d.zip       2           16      
3       f.doc       3           16      
4       g.doc       4           16  

When I want to query the item with the pg_id 16, I do this, 当我想用pg_id 16查询项目时,我这样做,

SELECT * 
FROM root_documents 
WHERE root_documents.pg_id = '16'

While when I want query the item without any pd_id, I do this, 虽然当我想要查询没有任何pd_id的项目时,我这样做,

SELECT * 
FROM root_documents 
WHERE root_documents.pg_id IS NULL

I find two queries are repetitive, so I try to this, 我发现两个查询是重复的,所以我尝试这个,

SELECT * 
FROM root_documents 
WHERE (root_documents.pg_id = ? OR root_documents.pg_id IS NULL)

So when I want to get this result only when I query the item with pg_id 16, 因此,当我想要使用pg_id 16查询项目时,我想获得此结果,

doc_id  doc_title   doc_order   pg_id 
12      c.zip       1           16  
13      d.zip       2           16      
3       f.doc       3           16      
4       g.doc       4           16

But I get all of them intead! 但我得到他们所有人!

doc_id  doc_title   doc_order   pg_id 
14      a.zip       1           NULL    
15      b.zip       2           NULL    
12      c.zip       1           16  
13      d.zip       2           16      
3       f.doc       3           16      
4       g.doc       4           16

How can I fix this query, 我该如何修复此查询,

SELECT * 
FROM root_documents 
WHERE (root_documents.pg_id = ? OR root_documents.pg_id IS NULL)

Or do I have to repeat the query like I usually do? 或者我必须像往常一样重复查询?

EDIT: 编辑:

This answer looks strange to me when I have a parameter to be passed into the placeholder: 当我有一个要传递给占位符的参数时,这个答案对我来说很奇怪:

SELECT * FROM root_documents WHERE (root_documents.pg_id = '16' OR (root_documents.pg_id IS NULL AND '16' IS NULL))

and when without a parameter, 当没有参数时,

SELECT * FROM root_documents WHERE (root_documents.pg_id = NULL OR (root_documents.pg_id IS NULL AND NULL IS NULL))

Note that I pass NULL into the placeholder when no value is returned. 请注意,当没有返回值时,我将NULL传递给placeholder

Sorry : you are using MYSQL... Disregard. 对不起:你正在使用MYSQL ......无视。

One way is this: 一种方法是:

SELECT * 
FROM root_documents 
WHERE isnull(root_documents.pg_id, -1) = ?

-1 or any value you never expect to find in [root_documents.pg_id]. -1或您希望在[root_documents.pg_id]中找不到的任何值。 Then when you want to search for NULL query for that value. 然后,当您要搜索该值的NULL查询时。

You could set the ? 你可以设置? to a number when you want that pg_id, or to the string IS NULL when you want the ones that are null. 当你想要那个pg_id时给一个数字,或者当你想要那些为null时给字符串IS NULL

I think you want something like this: 我想你想要这样的东西:

SELECT * 
FROM root_documents 
WHERE (root_documents.pg_id = ? OR (root_documents.pg_id IS NULL AND ? IS NULL))

I think the following should work for you( updated ): 我认为以下内容适合您( 更新 ):

SELECT * 
FROM root_documents r1
WHERE (r1.pg_id = ? 
OR (NOT EXISTS 
 (SELECT * FROM FROM root_documents r2 
  WHERE r2.pg_id = ? )
 AND r1.pg_id IS NULL)

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

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