[英]Very basic IF EXISTS statement not working
I really don't understand why my statement:我真的不明白为什么我的声明:
IF EXISTS (SELECT * FROM people WHERE ID = 168)
THEN SELECT * FROM people
END IF;
is returning this error:正在返回此错误:
Unknown statement type. (near "IF EXISTS" at position 0)
I'm using MariaDB 10.3.我正在使用 MariaDB 10.3。 Any ideas?有任何想法吗?
ADDITIONAL INFO附加信息
This is of course a simplified example.这当然是一个简化的例子。 What I wanna do is, concretely:我想做的是,具体地说:
IF EXISTS (SELECT * FROM people WHERE ID = 168)
THEN UPDATE people SET calculated_value = complex_queries_and_calculations
WHERE ID = 168
.., so to update a field of a given record if that record contains a given data, and else do nothing. ..,所以如果给定记录包含给定数据,则更新给定记录的字段,否则什么也不做。 To generate the data which would be used for the update, I need to query other tables for values and make some calculations.要生成将用于更新的数据,我需要查询其他表的值并进行一些计算。 I want to avoid these queries + calculations, if there's actually nothing to update.如果实际上没有什么可更新的话,我想避免这些查询+计算。 And in this case, simply do nothing.在这种情况下,什么都不做。 Hence, I guess that putting for example an EXIST
clause inside a WHERE
clause of the UPDATE
statement would end in many queries and calculations made in vain.因此,我猜想将例如EXIST
子句放在UPDATE
语句的WHERE
子句中将导致许多查询和计算失败。
MySQL and MariaDB only allow IF
statements in programming blocks -- stored functions, procedures, and triggers. MySQL 和 MariaDB 只允许编程块中的IF
语句——存储函数、过程和触发器。
Instead, just use:相反,只需使用:
select p.*
from people p
where exists (select 1 from people p2 where p2.id = 168);
This returns all people if id
168 is in table.如果id
168 在表中,这将返回所有人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.