How to put an IF statement in a MySQL query

I have a query that just selects from 3 tables.

  • select from the comments table all comments for the article

  • select UserID from the table to find which user he is

  • IF the user is of the same type, find one table ... ELSE find another table for final information.

How can i do this? I'm a little new to MySQL stuff, so I fully appreciate your patience!

PS - let me know if this is not clear ...

Thanks!

+3
source share
2 answers

, , - if "foo" "foovalues" " " "...." ,

 CommentID    ArticleID

   UserType

 Declare TestUserType varchar(3);

 select * from Comments where ArticleID = <inputid>; //Returns the comments 

 select TestUserType = UserType from Users where UserID = <inputuser>; //Returns the usertype for a user and assigns it to a variable

 if TestUserType = 'foo' 
   begin 
    select * from FooValues;
   end
 else
   begin 
    select * from FinalValues;
   end 

: SQL mySQL, , , , SQL .

SQL - , mySQL , , , -

+2

. , .

, , .

.

+1

Source: https://habr.com/ru/post/1703479/


All Articles