Since both columns in votes may be null you need to be careful. We can, therefore, check if any column from votes is null in the WHERE clause. In general, parentheses can be ignored in join expressions containing only inner join operations. INNER JOIN is used with an ON clause, CROSS JOIN is used otherwise. In standard SQL, they are not equivalent. If there are no rows that match the ON predicate's, all columns from votes is replaced with null in the result. In MySQL, JOIN, CROSS JOIN, and INNER JOIN are syntactic equivalents (they can replace each other). Since there may be nulls involved it is worth noting that the semantics differs between IN and EXISTS.įinally, you can use an outer join select election_id, title The NOT IN predicate can be used in a similar fashion. Hence my need to check if table (B) exists. My problem arises when there are no records retrieved due to the where conditions thus the table is not created. the election where it does not exists a vote from the user. After transposing my data I actually save the results into a table(B) so I can join with my main table(A). Even though you have not tagged your question, there is reason to believe that you are using MySQL, and MINUS or EXCEPT is not supported there.Īnother variant is to use the NOT EXISTS predicate: select election_id, title The result can be joined with elections to get the title of the elections. Select election_id from votes where user_id = ?įrom the set of elections, we remove those where the user has voted. Minus - except is used instead of minus by some vendors Perhaps the most straightforward way is to use a purely set-oriented approach: select election_id from elections There are a lot of ways to achieve what you are asking for.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |