1

I have the following code in the my stored procedure:

SET cont_email = SELECT email from contacts  WHERE id=in_id;

IF in_customer_email = cust_email then

**do something**

So, I was wondering if instead of creating a local variable and storing the result from the SELECT statement I just paste it directly in the if statement like so:

IF in_customer_email = (SELECT email from contacts  WHERE id=in_id) then

    **do something**

Also, I was wondering how I can use ORs in MySQL.

Can I do this?

IF in_customer_email = customer_email OR in_customer_name = customer_name THEN

I have seen the use of OR like this:

SELECT customer_id, customer_name
FROM customers
WHERE (customer_name = 'Apple' OR customer_name = 'Samsung')

but never inside of an if statement, so I wasn't sure.

And lastly, it has been ages since I could use something like if var1 = var2, since this is usually used for assigning not to compare, so I was wondering if this correct or I have to use double equal sign like var1 == var2.

Thanks.

VaTo
  • 2,936
  • 7
  • 38
  • 77
  • 1
    1. Yes 2. Yes 3. Single is fine. – kittykittybangbang Aug 31 '15 at 02:00
  • thanks for your answer @kittykittybangbang, I'm not so sure about my first question though. do I need semicolon at the end of the SELECT statement? `IF in_customer_email = (SELECT email from contacts WHERE id=in_id;) then` – VaTo Aug 31 '15 at 02:03
  • 1
    Negative, ghost rider. Leave that semicolon off. – kittykittybangbang Aug 31 '15 at 02:05
  • 1
    gheesh, @kittykittybangbang, write some answer up for lurking point bro – Drew Aug 31 '15 at 02:38
  • @Drew there's something scummy about accepting points for yes/no answers... but you talked me into it. ^_^ – kittykittybangbang Aug 31 '15 at 02:41
  • But consider how interesting the Answer could be made to sound, like how a used car salesman top of his field sells with Accepted Sincerity that he cares, that it is interesting, that he is genuinely concerned about your well being, and concerns – Drew Aug 31 '15 at 02:42
  • @Drew How's THAT for Accepted Sincerity? ;) – kittykittybangbang Aug 31 '15 at 02:47
  • 1
    very nice bangbang. I got a chuckle looking at [this](http://stackoverflow.com/q/710683/1816093) and [this](https://www.codecademy.com/blog/78-your-guide-to-semicolons-in-javascript) one too. Some people really get into writing a thesis on the topic. I went to Quora.com hilarious – Drew Aug 31 '15 at 02:50
  • @Drew Well, we've got to fill up all the pages of teh Interwebz somehow, right? ... >_ – kittykittybangbang Aug 31 '15 at 18:58
  • see @kittykittybangbang, that was worth the effort. 35 points and counting :> A few more like that, and no more begging to get those Edits accepted, they are automatic – Drew Aug 31 '15 at 18:59
  • @Drew and you actually get points when you edit a post and you don't have to wait that somebody has to approve it? – VaTo Aug 31 '15 at 21:35
  • no sir @SaulOrtega, it's all for the love of others at that point :> – Drew Aug 31 '15 at 21:44
  • so no more points for edits? that's a drawback at that level then :/ – VaTo Aug 31 '15 at 21:48
  • think how absurd it would be tho. Anyone can edit on top of each other. Pts aren't what we are here for. – Drew Aug 31 '15 at 21:54
  • Then how do you make points at that level? – VaTo Aug 31 '15 at 21:59
  • By answering questions like yours :3 – kittykittybangbang Aug 31 '15 at 22:02

1 Answers1

2

Variable vs. Nested Statement?

IF in_customer_email = (SELECT email from contacts  WHERE id=in_id) then

**do something**

This is a perfectly legal query -- no need to declare the variable beforehand.

ORs in IF Statements?

IF in_customer_email = customer_email OR in_customer_name = customer_name THEN

This is also perfectly legal (and often quite useful).

Use = or == to Compare Values?

if var1 = var2

This is, contrary to what your programmer spidey-sense may indicate, the proper way to compare values in MySQL queries.

Community
  • 1
  • 1
kittykittybangbang
  • 2,380
  • 4
  • 16
  • 27