Welcome to Dream.In.Code
Become an Expert!

Join 149,748 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,422 people online right now. Registration is fast and FREE... Join Now!




MySQL: Check if two values are equal

 
Reply to this topicStart new topic

MySQL: Check if two values are equal

girasquid
19 Feb, 2007 - 05:00 PM
Post #1

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
Hello, all.

For a recent project, I have been tasked with taking a count of the number of rows in two MySQL tables, and then figuring out if they are equal. I know that I can do this by using two queries, retrieving the values, and comparing them, but I'm wondering: is there a way to do it that uses only one query?

Thanks,
Girasquid
User is offlineProfile CardPM
+Quote Post

snoj
RE: MySQL: Check If Two Values Are Equal
19 Feb, 2007 - 05:24 PM
Post #2

Fell off the face of the earth
Group Icon

Joined: 31 Mar, 2003
Posts: 3,325



Thanked: 9 times
Dream Kudos: 750
My Contributions
Sounds like homework to me, but to answer your question, yes, it can be done in one query.
User is offlineProfile CardPM
+Quote Post

girasquid
RE: MySQL: Check If Two Values Are Equal
19 Feb, 2007 - 05:33 PM
Post #3

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,294



Thanked: 18 times
Dream Kudos: 725
My Contributions
QUOTE(hotsnoj @ 19 Feb, 2007 - 06:24 PM) *

Sounds like homework to me, but to answer your question, yes, it can be done in one query.


It isn't homework, but this is the query I've tried and had fail:
CODE

(SELECT COUNT(*) FROM user_answered WHERE user = ?) = (SELECT COUNT(*) FROM questions)


That throws an error about my SQL syntax, and I'm also pretty sure I'm looking for something else syntactically to do this.

EDIT: I figured out how to do it on my own, it turns out I needed to put a SELECT in front of that query, so that it looks like this:
CODE

SELECT (SELECT COUNT(*) FROM user_answered WHERE user = ?) = (SELECT COUNT(*) FROM questions)

Which will return a 0 if they don't match, and a 1 if they do.

This post has been edited by girasquid: 19 Feb, 2007 - 06:06 PM
User is offlineProfile CardPM
+Quote Post

Trogdor
RE: MySQL: Check If Two Values Are Equal
20 Feb, 2007 - 03:21 AM
Post #4

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 549



Thanked: 4 times
Dream Kudos: 125
My Contributions
Great that you found it out yourself.

Tip: You can change the output behaviour with the CASE WHEN construct. (for example to give 'Y' or 'N' instead of 1 or 0)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 05:39AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month