我为下面的代码创建了一个查询,该查询应该返回满足两个条件的记录:
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.
更多推荐
发布评论