if statement - MySQL: check if data exist inside query -
i never used "if-else" or "case" inside sql-query, guess need time. have table data represents competition between 2 users:
//properties of "competition-table int competitionid int userid_contrahent1 int userid_contrahent2 otherdata.... users can vote 1 or other contrahent within competition; vote represented so:
//properties of vote-table int voteid int competitionid int userid_voter int userid_winner // user vote counts otherdata.... obviously every given user in webapplication can vote once given competition. when query competition want have information if currentuser (which owns current session) voted competition. in addition other properties of competition have additional one, has key "voted" , value "yes" or "no". select statement should guess:
select competition.*, if exists ( select * vote userid_voter = $currentuserid , competitionid = competition.competitionid) ...do competition; how do in mysql?
select c.*, if(v.competitionid not null, 'voted', 'not voted') `verdict` competition c left join vote v on v.competitionid = c.competitionid , v.userid_voter = $currentuserid
Comments
Post a Comment