SQL Best Practices

From Littledamien Wiki
Revision as of 11:28, 7 January 2022 by Video8 (talk | contribs) (Created page with "== Determining if a record exists == <syntaxhighlight lang="sql"> SELECT EXISTS (SELECT 1 FROM users_table WHERE user_table.userid = p_userId) INTO p_isPresent; </syntaxhighlight> Not <syntaxhighlight lang="sql"> SELECT COUNT(*) FROM users_table WHERE user_table.userid = p_userId INTO p_isPresent; </syntaxhighlight> `SELECT EXISTS` stops as soon as it finds a match. `COUNT` counts all matches. Category:MySQLCategory:MariaDB")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Determining if a record exists[edit]

SELECT EXISTS (SELECT 1 FROM users_table WHERE user_table.userid = p_userId) INTO p_isPresent;

Not

SELECT COUNT(*) FROM users_table WHERE user_table.userid = p_userId INTO p_isPresent;

SELECT EXISTS stops as soon as it finds a match. COUNT counts all matches.