LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 07-26-2008, 03:46 AM   #1
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Rep: Reputation: 30
using if inside mysql query


Hi,

I've been trying to execute this query, but it doesn't give me what I need:

PHP Code:
select predicts.*,matchResult.match_id,matchResult.team1_id,

matchResult.team2_id,matchResult.date,teams1.team as 
team1,teams2.team as team2 from matchResult LEFT JOIN
teams 
as teams1 on teams1.team_id matchResult.team1_id
LEFT JOIN teams 
as teams2 on teams2.team_id matchResult.team2_id


LEFT join predicts on predicts
.match_id=matchResult.match_id

where matchResult
.date>now() and matchResult.status=and 
IF(
predicts.match_id is 
NULL
,predicts.user_id='2',predicts.user_id='1')
order by matchResult.date 
I think the important part of the query starts from LEFT join predicts.

Right now, this query returns its value with predicts.user_id='1' condition.

Here is what I'm looking for using any programming language:

PHP Code:
if (matchResult.date>now && matchResult.status==2)
{
     if (
predicts.user_id == 2)
                
condition="condition 2"
     
else
         
condition="default Condition" //predicts.user_id=1
     


Here is a simple illustration of my problem with numbers:


PHP Code:
TableMatchResult
Data
:
columnmatch_id 1,2,3,4,5,6,7,8


Table
predicts
Data
:
columnmatch_id 1,2,3,4,5,6,7,8
column
user_id  1,1,1,1,1,1,1,1
column
result1  NULL,NULL,NULL,NULL,NULL,NULL

column
match_id 1,2,5
column
user_id  2,2,2
column
result1  3,4,
As you can see, matchResult table has 8 records. predicts always has those match_id available in matchResult table for user_id=1 while result1 for user1 is always null.

What I'm looking for is to select all 8 matches from matchResult table and if user 2 has an entry in it, output that entry, otherwise show that of user 1 which is always there.

This is what I'm expecting to see from the figure above:

PHP Code:
match_id1,2,3   ,4   ,5,6   ,7   ,8
user_id
:  2,2,1   ,1   ,2,1   ,1   ,1
result1
:  3,4,NULL,NULL,1,NULL,NULL,NULL 
As you can see, I need to prioritize my output. If user 2 has an entry for that corresponding match, output records for user2, otherwise give that of user1 which is always there.

I'm very sorry for such a long description. I have been working on this to get it working for a long time with no success.

I would be glad if you share your thoughts with me.

Thanks in advance.

Last edited by mohtasham1983; 07-26-2008 at 03:48 AM.
 
Old 07-26-2008, 09:29 AM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
I am not familiar with the IF syntax, never used it. Have you tried a subquery?

This pretty much looks like the first example for subquery syntax in the MySQL docs.

http://dev.mysql.com/doc/refman/5.0/en/subqueries.html

If that doesn't help, please correct some ambiguity in your explanation:

In the first code box in the IF statement you want to decide on predicts.match_id, but in the pseudo code in the second box you use predicts.user_id to branch. Which is correct?

Also, in the third box, the number example it is not clear to what the three last lines belong. Is that just a different case of the contents of the predicts table?

What is the description of the predicts table? Is the only result column result1?

Is it correct that if the value of Result1 != NULL you want to output the record from predicts where user_id=2?

Maybe the best explanation is to post all the real contents of each table, and create manually an output of what you want to see from the query.

jlinkels
 
Old 07-26-2008, 11:20 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,376

Rep: Reputation: 147Reputation: 147
Not certain if it will work, but can you group by user_id and use as the user_id field MAX(user_id)
 
Old 07-27-2008, 01:59 AM   #4
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
Alright, I think I'm very close to the result that I'm looking for. I used a union to join the result of both queries. Right now I need to distinct match_id.

here is the actual data available in predicts table:

PHP Code:
+------------+----------+---------+---------+---------+---------------------+-------------+----------+----------+
predict_id match_id user_id result1 result2 date                isPredicted lockDate isLocked |
+------------+----------+---------+---------+---------+---------------------+-------------+----------+----------+
|         
29 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        
|         
30 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        
|         
31 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        
|         
32 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        
|         
33 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        
|         
34 |       10 |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        
|         
35 |       11 |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        
|         
36 |       12 |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        
|         
37 |       13 |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        
|         
38 |        |       |       |       2008-07-25 23:07:35 |           NULL     |        
|         
39 |        |       |       |       2008-07-25 23:17:21 |           NULL     |        
+------------+----------+---------+---------+---------+---------------------+-------------+----------+----------+ 
and Here is the data available in matchResult table:

PHP Code:
+----------+-----------+------------+----------+----------+---------+---------+---------------------+--------+
match_id league_id weekNumber team1_id team2_id result1 result2 date                status |
+----------+-----------+------------+----------+----------+---------+---------+---------------------+--------+
|        
|         |          |       92 |       94 |    NULL |    NULL 2008-08-04 05:30:00 |      
|        
|         |          |       99 |      100 |    NULL |    NULL 2008-08-05 17:00:00 |      
|        
|         |          |      104 |       98 |    NULL |    NULL 2008-08-05 17:00:00 |      
|        
|         |          |      105 |      107 |    NULL |    NULL 2008-08-05 17:00:00 |      
|        
|         |          |      109 |       96 |    NULL |    NULL 2008-08-05 17:00:00 |      
|       
10 |         |          |      111 |      103 |    NULL |    NULL 2008-08-05 17:00:00 |      
|       
11 |         |          |       95 |       97 |    NULL |    NULL 2008-08-05 17:00:00 |      
|       
12 |         |          |      101 |      110 |    NULL |    NULL 2008-08-05 17:00:00 |      
|       
13 |         |          |       93 |      108 |    NULL |    NULL 2008-08-05 17:00:00 |      
+----------+-----------+------------+----------+----------+---------+---------+---------------------+--------+ 
This query
PHP Code:
(select predicts.*,matchResult.match_id,matchResult.team1_id,
matchResult.team2_id,matchResult.date,teams1.team as team1,teams2.team as team2 from matchResult LEFT JOIN
 teams 
as teams1 on teams1.team_id matchResult.team1_id LEFT JOIN
 teams 
as teams2 on teams2.team_id matchResult.team2_id LEFT join
 predicts on predicts
.match_id=matchResult.match_id where
 matchResult
.date>now() and matchResult.status=and 
predicts.user_id='2' order by matchResult.dateunion
 
(select predicts.*,matchResult.match_id,matchResult.team1_id,
matchResult.team2_id,matchResult.date,teams1.team as team1,teams2.team as team2 from matchResult LEFT JOIN teams as
 
teams1 on teams1.team_id matchResult.team1_id LEFT JOIN teams

 
as teams2 on teams2.team_id matchResult.team2_id LEFT join 
predicts on predicts
.match_id=matchResult.match_id where 
matchResult
.date>now() and matchResult.status=and 
predicts.user_id='1' order by matchResult.date

returns the following:
PHP Code:
+------------+----------+---------+---------+---------+---------------------+-------------+----------+----------+----------+----------+----------+---------------------+---------------------------+---------------------------+
predict_id match_id user_id result1 result2 date                isPredicted lockDate isLocked match_id team1_id team2_id date                team1                     team2                     |
+------------+----------+---------+---------+---------+---------------------+-------------+----------+----------+----------+----------+----------+---------------------+---------------------------+---------------------------+
|         
38 |        |       |       |       2008-07-25 23:07:35 |           NULL     |        |        |       92 |       94 2008-08-04 05:30:00 | سايپا                | پرسپوليس          | 
|         39 |        |       |       |       2008-07-25 23:17:21 |           NULL     |        |        |      109 |       96 2008-08-05 17:00:00 | پاس همدان         | سپاهان              | 
|         29 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        |        |       92 |       94 2008-08-04 05:30:00 | سايپا                | پرسپوليس          | 
|         30 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        |        |       99 |      100 2008-08-05 17:00:00 | ذوب آهن             | مس کرمان           | 
|         31 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        |        |      104 |       98 2008-08-05 17:00:00 | صبا باتری         | پيکان                | 
|         32 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        |        |      105 |      107 2008-08-05 17:00:00 | ملوان                | راه آهن             | 
|         33 |        |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        |        |      109 |       96 2008-08-05 17:00:00 | پاس همدان         | سپاهان              | 
|         34 |       10 |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        |       10 |      111 |      103 2008-08-05 17:00:00 | پيام ارتباطات | برق                    | 
|         35 |       11 |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        |       11 |       95 |       97 2008-08-05 17:00:00 | استقلال            | ابومسلم            | 
|         36 |       12 |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        |       12 |      101 |      110 2008-08-05 17:00:00 | فجرشيراز          | فولاد خوزستان | 
|         37 |       13 |       |    NULL |    NULL 2008-07-25 23:01:55 |           NULL     |        |       13 |       93 |      108 2008-08-05 17:00:00 | استقلال اهواز | پگاه                  | 
+------------+----------+---------+---------+---------+---------------------+-------------+----------+----------+----------+----------+----------+---------------------+---------------------------+---------------------------+
11 rows in set (0.00 sec

What I'm looking for is the same as above, except that I don't want to see a mtach_id for user_id 1 if user_id 2 has a value for that match_id.
It should be something like this:

Last edited by mohtasham1983; 07-27-2008 at 02:02 AM.
 
Old 07-27-2008, 07:57 PM   #5
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
Quote:
Originally Posted by mohtasham1983 View Post

What I'm looking for is the same as above, except that I don't want to see a mtach_id for user_id 1 if user_id 2 has a value for that match_id.
It should be something like this:
I think some part of your post got lost. I am lost myself pretty much as well, but I find it an interesting problem and would like to help you with the SQL statement.

Can you please try and post the output you expect, using the actual data of you last post?

jlinkels
 
Old 07-27-2008, 08:27 PM   #6
mohtasham1983
Member
 
Registered: Apr 2005
Location: San Jose
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408

Original Poster
Rep: Reputation: 30
Thank you very much for your interest. After two days, just now I found a query to do what I'm looking for. I used a nasty trick to get it working.

Here is what I did:

Suppose I have the following information in my predicts table:

PHP Code:
+------------+----------+---------+---------+---------+---------------------+-------------+----------+----------+
predict_id match_id user_id result1 result2 date                isPredicted lockDate isLocked |
+------------+----------+---------+---------+---------+---------------------+-------------+----------+----------+
|         
50 |        |       |    NULL |    NULL 2008-07-27 18:12:58 |           NULL     |        
|         
51 |        |       |    NULL |    NULL 2008-07-27 18:12:58 |           NULL     |        
|         
52 |        |       |    NULL |    NULL 2008-07-27 18:12:58 |           NULL     |        
|         
53 |        |       |    NULL |    NULL 2008-07-27 18:12:58 |           NULL     |        
|         
54 |        |       |    NULL |    NULL 2008-07-27 18:12:58 |           NULL     |        
|         
55 |       10 |       |    NULL |    NULL 2008-07-27 18:12:58 |           NULL     |        
|         
56 |       11 |       |    NULL |    NULL 2008-07-27 18:12:58 |           NULL     |        
|         
57 |       12 |       |    NULL |    NULL 2008-07-27 18:12:58 |           NULL     |        
|         
58 |       13 |       |    NULL |    NULL 2008-07-27 18:12:58 |           NULL     |        
|         
59 |        |     968 |       |       2008-07-27 18:13:42 |           NULL     |        
|         
60 |        |     968 |       |       2008-07-27 18:13:55 |           NULL     |        
|         
61 |       13 |     968 |       |       2008-07-27 18:14:06 |           NULL     |        
|         
62 |        |       |       |       2008-07-27 18:20:07 |           NULL     |        
+------------+----------+---------+---------+---------+---------------------+-------------+----------+----------+
13 rows in set (0.00 sec
The following relatively simple query gives me what I'm looking for:

PHP Code:
select predicts.*,matchResult.match_id,matchResult.team1_id,matchResult.team2_id,
matchResult.date,teams1.team as team1,teams2.team as team2 from matchResult
 LEFT JOIN teams 
as teams1 on teams1.team_id matchResult.team1_id
 LEFT JOIN teams 
as teams2 on teams2.team_id matchResult.team2_id
 LEFT join predicts using 
(match_idwhere matchResult.date>now()
 and 
matchResult.status=and (predicts.user_id ='{$user_id}'
 
and predicts.isPredicted='1') or predicts.user_id ='1'
 
group by predicts.match_id desc order by matchResult.date 
This query works properly if user_id=1 is always there before any other user_id. user_id=1 is a dummy user id that makes things a lot easier and it prevents me from dealing with NULLs. However, this user id should be inserted into database before any other user enters its predicts. I wrote a simple script to do that automatically whenever new matches are available so that it can populate database before any other user can do that.

I know it's a bad idea to use such tricks, but I got everything working using this method and it made me satisfied after two days of writing large number of queries, some of them as long as 30 lines.
 
Old 07-27-2008, 09:02 PM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,053

Rep: Reputation: 484Reputation: 484Reputation: 484Reputation: 484Reputation: 484
The quiery certainly is elegant now. I don't see anything wrong with a user_id=1 to signal that the user is not valid. Although I mostly prefer to use user_id=0 for something non-existent, and start the users table with user_id=1. But that is a matter of preference and as long as you use a consistent value for an invalid (or incomplete record).

I don't like to use NULL for a non-initialized field. First you have to use the ISNULL function in your WHERE clause (may be changed as of PHP 5), and secondly you cannot simple say WHERE user_id > 0 and user_id < 5 or something like that.

In PHP it is a nuisance to deal with NULL values, if you get something returned with NULL and you attempt to use that array element, an error is raised. (If I remember well, the might be other issues, it is version dependent as well)

jlinkels
 
  


Reply

Tags
if, query, sql, statement


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
mysql: need help with query BrianK Programming 8 07-09-2008 09:10 PM
interesting MySQL query/view query :s mjh Programming 3 03-25-2008 07:30 AM
mysql use output of one query in another query secretlydead Programming 2 11-19-2007 01:25 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM
Mysql Error: Lost Connection to Mysql during query ramnath Programming 5 11-18-2003 12:27 PM


All times are GMT -5. The time now is 07:54 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration