ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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=2 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:
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:
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.
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.
Distribution: Fedora 3,4- Ubuntu 6.06 to 8.10, Gentoo and Arch
Posts: 408
Original Poster
Rep:
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:
(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=2 and predicts.user_id='2' order by matchResult.date) union (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=2 and predicts.user_id='1' order by matchResult.date)
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.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Quote:
Originally Posted by mohtasham1983
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?
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_id) where matchResult.date>now() and matchResult.status=2 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.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.