嵌套联合查询(Nested conjunctive query)

编程入门 行业动态 更新时间:2024-10-27 21:16:33
嵌套联合查询(Nested conjunctive query)

我为下面的代码创建了一个查询,该查询应该返回满足两个条件的记录:

1- ID等于#7

2-疾病等于单引号之间写的疾病之一

我正在应用此查询的数据库中ID = 7的记录没有查询中列出的任何疾病作为属性“疾病”的值,因此查询不应返回任何记录作为结果。 然而,作为查询的结果,它返回ID = 7的元组。 你能告诉我我的代码有什么问题吗? 我尝试用(OR)替换(||),但我仍然得到相同的结果。

SELECT * FROM patients WHERE disease = ( 'migraine' || 'stroke' || 'concussion' || 'down_syndrome' || 'epilepsy' || 'autism' || 'hydrocephalus' || 'dyslexia' || 'dystonia' || 'aphasia' || 'coma' || 'aneurysm' || 'batten_disease' || 'brain_cancer' || 'alzheimers_disease' || 'amyolrophic_alteral_sclerosis' ) AND ID = '7'

I created the code below for a query that is supposed to return the records that satisfies two conditions:

1- ID is equal to #7

2- Disease is equal to one of the diseases written between the single quotes

The record with ID = 7 in the database I'm applying this query on doesn't have any of the diseases listed in the query as a value for the attribute "disease", so the query shouldn't return any record as a result. Yet it is returning the tuple with ID = 7 as a result for the query. Can you please tell me what is the problem with my code? I tried replacing the (||) with the word (OR) but I'm still getting the same result.

SELECT * FROM patients WHERE disease = ( 'migraine' || 'stroke' || 'concussion' || 'down_syndrome' || 'epilepsy' || 'autism' || 'hydrocephalus' || 'dyslexia' || 'dystonia' || 'aphasia' || 'coma' || 'aneurysm' || 'batten_disease' || 'brain_cancer' || 'alzheimers_disease' || 'amyolrophic_alteral_sclerosis' ) AND ID = '7'

最满意答案

你的意思是使用像IN运算符

SELECT * FROM patients WHERE disease IN ( 'migraine', 'stroke' , 'concussion' , 'down_syndrome' , 'epilepsy' , 'autism' , 'hydrocephalus' , 'dyslexia' ,'dystonia' , 'aphasia' , 'coma' ,'aneurysm' , 'batten_disease' , 'brain_cancer' , 'alzheimers_disease' , 'amyolrophic_alteral_sclerosis' ) AND ID = '7';

编辑:每条评论: || 是一个连接操作符,而不是OR条件,所以最终你得到一个字符串与所有疾病连在一起,你想要比较。

You meant to use an IN operator like

SELECT * FROM patients WHERE disease IN ( 'migraine', 'stroke' , 'concussion' , 'down_syndrome' , 'epilepsy' , 'autism' , 'hydrocephalus' , 'dyslexia' ,'dystonia' , 'aphasia' , 'coma' ,'aneurysm' , 'batten_disease' , 'brain_cancer' , 'alzheimers_disease' , 'amyolrophic_alteral_sclerosis' ) AND ID = '7';

Edit: per comment: || is a concatenation operator and not a OR condition and so ultimately you getting a string concatanated with all diseases and that you are trying to compare.

更多推荐

本文发布于:2023-08-04 16:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1417871.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:嵌套   Nested   query   conjunctive

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!