简体   繁体   English

SQL在多个表上选择EXIST作为布尔值(位)

[英]SQL select EXIST over multiple tables as Boolean (Bit)

I am using the accepted answer from this question , but for multiple tables. 我正在使用这个问题的公认答案,但是用于多个表。

$result = mssql_query("SELECT CAST(
       CASE WHEN 
         EXISTS(select 1 from codes where code = '".$generatedCode."') OR 
         EXISTS(select 1 from pakete where code = '".$generatedCode."') OR
         EXISTS(select 1 from kunden where code = '".$generatedCode."') OR
         EXISTS(select 1 from formulare where code = '".$generatedCode."') OR  
         EXISTS(select 1 from berater where code = '".$generatedCode."') 
       THEN 1 
       ELSE 0 
       END 
    AS BIT) as 'exists'";

$row = mssql_fetch_assoc($result);
if ($row['exists']==0)

Theoretically it looks good, practically it works too. 从理论上讲,它看起来不错,实际上也可以。 But it happens from time to time, that the query (i think) wents wrong and returns 1 even if there is a "code" in one of the tables. 但是有时会发生这样的情况,即查询(我认为)出错并返回1,即使其中一个表中有“代码”也是如此。

Maybe someone knows a better solution for checking across tables, where it could happen that the searched varchar exists in one or two tables only. 也许有人知道跨表检查的更好解决方案,在这种情况下,搜索到的varchar可能仅存在于一个或两个表中。

fyi: generatedcode is a nine digit alphanumeric unique string. fyi:generatecode是一个九位数的字母数字唯一字符串。

Regards, Markus 问候,马库斯

UPDATE: The Query works as expected, the failure was a wrong named variable after return, so the original generatedcode (the duplicate one) was used instead. 更新:查询按预期方式工作,失败是返回后错误的命名变量,因此使用了原始生成的代码(重复的代码)。 Thanks for your help and for simplifying the query. 感谢您的帮助并简化了查询。

You know, you can use simple union and num_rows, there's no need to select a bit - it won't do you any good unless your query is a part of other complex query: 您知道,可以使用简单的union和num_rows,不需要选择一点-除非您的查询是其他复杂查询的一部分,否则它不会有任何好处:

$sql="select 1 from codes where code = '".$generatedCode."' 
union select 1 from pakete where code = '".$generatedCode."'
union select 1 from kunden where code = '".$generatedCode."'
union select 1 from formulare where code = '".$generatedCode."'
union select 1 from berater where code = '".$generatedCode."'";
$result = mssql_query($sql);
if(mssql_num_rows($result)){}

I am guessing that your code works. 我猜您的代码有效。 You can do a couple things to improve it. 您可以做一些事情来改善它。 First, the cast() isn't necessary because you can put in bit literals directly (see here ). 首先,不需要cast()因为您可以直接输入位文字(请参阅此处 )。 The following code uses both methods for expressing a bit literal. 以下代码使用两种方法来表达bit文字。

Second, don't use single quotes for column names (and, of course, mysql_ is deprecated, but that is another matter because this just focuses on the query). 其次,不要对列名使用单引号(当然,不建议使用mysql_,但这是另一回事,因为它只关注查询)。 In fact, you should avoid using reserved words as column names. 实际上,您应该避免使用保留字作为列名。 So: 所以:

SELECT (CASE WHEN 
         EXISTS(select 1 from codes where code = '".$generatedCode."') OR 
         EXISTS(select 1 from pakete where code = '".$generatedCode."') OR
         EXISTS(select 1 from kunden where code = '".$generatedCode."') OR
         EXISTS(select 1 from formulare where code = '".$generatedCode."') OR  
         EXISTS(select 1 from berater where code = '".$generatedCode."') 
         THEN 0b1 
         ELSE  b'0' 
       END) as CodeExists

By the way, I would discourage you from using bits, unless you really know what you are doing in terms of optimization. 顺便说一句,除非您真的了解优化方面的工作,否则我不鼓励您使用位。 They may not save space and they may not run faster. 它们可能无法节省空间,并且运行速度可能不会更快。 The following seems totally reasonable to me: 以下对我来说似乎完全合理:

SELECT (EXISTS(select 1 from codes where code = '".$generatedCode."') OR 
        EXISTS(select 1 from pakete where code = '".$generatedCode."') OR
        EXISTS(select 1 from kunden where code = '".$generatedCode."') OR
        EXISTS(select 1 from formulare where code = '".$generatedCode."') OR  
        EXISTS(select 1 from berater where code = '".$generatedCode."') 
       ) as CodeExists

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

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