Multiple results

Reply

Join Date: Feb 2006
Posts: 7
Reputation: lux is an unknown quantity at this point 
Solved Threads: 0
lux lux is offline Offline
Newbie Poster

Multiple results

 
0
  #1
Feb 27th, 2006
Hello,

I have this query:

SELECT players.id AS id, players.name AS name, players.surname AS surname, players.email AS email, calendar.start AS beginning, calendar.end AS finnish, calendar.name AS title, calendar.event AS description, calendar.id AS eventid FROM `calendar` , `attendance` , `players` WHERE (attendance.present = "yes" AND calendar.start BETWEEN "1141060879" AND "1141665679" AND attendance.event = calendar.id AND attendance.player = players.id) OR (calendar.added BETWEEN "1140974479" AND "1141060879") ORDER BY players.id,calendar.start ASC LIMIT 0 , 30



The query returns the values I want, but many times the same rows (6 times each), does anyone know why and/or how to fix it?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Multiple results

 
0
  #2
Mar 8th, 2006
It's called a "Cartesian product".
Mathematical explanation: http://en.wikipedia.org/wiki/Cartesian_product
SQL explanation: http://www.fluffycat.com/SQL/Cartesian-Joins/

Try this:
---------------------------------------------------
SELECT p.id
, p.name
, p.surname
, p.email
, c.start AS beginning
, c.end AS finnish
, c.name AS title
, c.event AS description
, c.id AS eventid
FROM calendar c
INNER JOIN attendance a ON a.event = c.id
INNER JOIN players p ON p.id = a.player
WHERE a.present = "yes"
AND ((c.start >= '1141060879'
AND c.start <= '1141665679')
OR (c.added >= '1140974479'
AND c.added <= '1141060879'))
ORDER BY p.id ASC
, c.start ASC
LIMIT 0, 30
---------------------------------------------------

Notice that I did not name all the colums using "AS". There is no need to specify a name for a column if the name is the same as the column name. Also notice I used table aliases to shorten the code. I used the JOIN methods instead of the WHERE clause join methods. It seems hard at first, but after you get used to it, you'll find the joins make a lot more sense to you and keeps the WHERE clauses straight forward. (My opinion of course.)
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 7
Reputation: lux is an unknown quantity at this point 
Solved Threads: 0
lux lux is offline Offline
Newbie Poster

Re: Multiple results

 
0
  #3
Mar 8th, 2006
  1. SELECT p.id
  2. , p.name
  3. , p.surname
  4. , p.email
  5. , c.start AS beginning
  6. , c.END AS finnish
  7. , c.name AS title
  8. , c.event AS description
  9. , c.id AS eventid
  10. FROM calendar c
  11. INNER JOIN attendance a ON a.event = c.id
  12. INNER JOIN players p ON p.id = a.player
  13. WHERE (
  14. c.added >= '1141740333'
  15. AND c.added <= '1141826733'
  16. )
  17. ORDER BY p.id ASC
  18. , c.start ASC
  19. LIMIT 0, 30

Hey Troy,

First of all A BIG thanks for your comment. It really lightened up my mind a lot. Your one comment is better than the dozens of others I have recieved in other forums. But one thing hasn't really worked out, the query above won't results. I tried removing some parts of your query but this one in particular doesn't work. Below I included the database:
  1. --
  2. -- Table structure for table `attendance`
  3. --
  4.  
  5. CREATE TABLE `attendance` (
  6. `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  7. `event` INT(10) UNSIGNED NOT NULL DEFAULT '0',
  8. `player` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
  9. `present` ENUM('yes','no') NOT NULL DEFAULT 'no',
  10. PRIMARY KEY (`id`)
  11. ) TYPE=MyISAM PACK_KEYS=0 AUTO_INCREMENT=2 ;
  12.  
  13. --
  14. -- Dumping data for table `attendance`
  15. --
  16.  
  17. INSERT INTO `attendance` VALUES (1, 1, 2, 'yes');
  18.  
  19. -- --------------------------------------------------------
  20.  
  21. --
  22. -- Table structure for table `calendar`
  23. --
  24.  
  25. CREATE TABLE `calendar` (
  26. `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  27. `name` VARCHAR(128) NOT NULL DEFAULT '',
  28. `author` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
  29. `event` TEXT NOT NULL,
  30. `start` INT(16) NOT NULL DEFAULT '0',
  31. `END` INT(16) NOT NULL DEFAULT '0',
  32. `added` INT(16) NOT NULL DEFAULT '0',
  33. PRIMARY KEY (`id`)
  34. ) TYPE=MyISAM PACK_KEYS=1 AUTO_INCREMENT=2 ;
  35.  
  36. --
  37. -- Dumping data for table `calendar`
  38. --
  39.  
  40. INSERT INTO `calendar` VALUES (1, 'Birthday', 1, 'It''s my birthday!', 1141999200, 1142006400, 1141826592);
  41.  
  42. -- --------------------------------------------------------
  43.  
  44. --
  45. -- Table structure for table `players`
  46. --
  47.  
  48. CREATE TABLE `players` (
  49. `id` TINYINT(3) UNSIGNED NOT NULL AUTO_INCREMENT,
  50. `name` VARCHAR(128) NOT NULL DEFAULT '',
  51. `surname` VARCHAR(128) NOT NULL DEFAULT '',
  52. `email` VARCHAR(128) NOT NULL DEFAULT '',
  53. `password` VARCHAR(128) NOT NULL DEFAULT '',
  54. `homephone` VARCHAR(128) NOT NULL DEFAULT '',
  55. `mobile` VARCHAR(128) NOT NULL DEFAULT '',
  56. PRIMARY KEY (`id`)
  57. ) TYPE=MyISAM PACK_KEYS=1 AUTO_INCREMENT=3 ;
  58.  
  59. --
  60. -- Dumping data for table `players`
  61. --
  62.  
  63. INSERT INTO `players` VALUES (1, 'John', 'Doe', 'john@doe.com', 'md5password', '123456789', '8768686865');
  64. INSERT INTO `players` VALUES (2, 'Bob', 'the Builder', 'bob@builder.com', 'thisismypassword', '987654321', '989797976');

Thanks,

Sam
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Multiple results

 
0
  #4
Mar 8th, 2006
Sam, good work sending me the full structure with sample data. I simply ran that against my MySql server and ended up with your 3 tables and the sample data. Then I ran the original query I sent you. I did not get any errors, but I did not get any data. This means the query is syntactically correct. Whew.

Then I simply examined the data to see why nothing matched the query. The reason is that the calendar start and add values are outside the range requested in the WHERE clause. I simply changed the last number in the where clause to 1151060879, and walla--it works.

Curious--I assume the 'start', 'end', and 'added' columns are datetime data? Why are you using int(16) instead of DATETIME?
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 7
Reputation: lux is an unknown quantity at this point 
Solved Threads: 0
lux lux is offline Offline
Newbie Poster

Re: Multiple results

 
0
  #5
Mar 8th, 2006
Originally Posted by Troy
Then I simply examined the data to see why nothing matched the query. The reason is that the calendar start and add values are outside the range requested in the WHERE clause. I simply changed the last number in the where clause to 1151060879, and walla--it works.
Hey Troy,

But when you got results, did you get Bob and John or did you only get Bob (like I did?). I was hoping to get both...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Multiple results

 
0
  #6
Mar 8th, 2006
I only got Bob, and you should only get Bob. Look at your sample data--the only player referenced in the attendance table is Bob. Since we are INNER JOINing with attendance, we'll only get players who are in the attendance table.

It is possible to do an "OUTER JOIN" where all rows of one of the tables show up regardless of whether they have any data in the joined tables, but it is relatively rare that your business logic would need that. If you do determine that you truly need an OUTER JOIN in this situation, let me know.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 7
Reputation: lux is an unknown quantity at this point 
Solved Threads: 0
lux lux is offline Offline
Newbie Poster

Re: Multiple results

 
0
  #7
Mar 8th, 2006
Basically I'm trying to retrieve all the results of the events added during the last 24 hours and all those that will occur during the next week and people have said they were coming. Then all this is processed in php and e-mailed. What's the best way?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Multiple results

 
0
  #8
Mar 8th, 2006
Well, without fully knowing your business, first, I'd make those start, end, and added columns type DATETIME. I'd adjust my logic to store actual timestamps in those columns. That way, the data is human-readable. You'd store values in this format '2006-03-06 14:21:08' You can query the data ranges just like you do now with the benefit that you can read the timestamps when viewing the data using tools like phpMyAdmin.

If changing those columns is not an option at this point, it's not a showstopper--it will work (I assume you are storing the unix integer time value instead of the timestamp format.)

I'm trying to retrieve all the results of the events added during the last 24 hours and all those that will occur during the next week and people have said they were coming.
Hmmmm, I'm guessing here, but what you describe sounds like 2 jobs to me.
  1. You want to be able to notify all players of new events added.
  2. If events are coming up in the next week, notify all players who have indicated they will attend.
I would do this using two seperate logic loops and two seperate queries because these are very different tasks.

In fact, I'd probably do this in three queries. For the first job, I'd probably query the players and put their names and email addresses into a PHP array. Then I'd query the calendar table for new events (added in last 24 hours). Then I'd send an email to all players about those new events. The third query would be the one I already gave you except you'd remove the added part of the where clause. This query finds upcoming events and those players who have said they'll attend. Problem is, if a player is signed up for more than one upcoming event, you'll have their email address in the results twice. This is not a problem really--just a fact of the data. So as you loop over the results in your PHP, you'll need to make sure you only send one email per email address.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 7
Reputation: lux is an unknown quantity at this point 
Solved Threads: 0
lux lux is offline Offline
Newbie Poster

Re: Multiple results

 
0
  #9
Mar 8th, 2006
So is the outer join not a possibility to achieve this? As that would be an easier script. For the moment I was very close to achieving it with the existing query, as the only thing lacking was the recently added events. Otherwise I could maybe create a new query for it but then I would have to somehow send it n the same e-mail as many e-mails would be very inconvenient...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Multiple results

 
0
  #10
Mar 8th, 2006
I'm fairly confident it is not possible to achieve what you want with a single query. Other SQL gurus are welcome to jump in to prove me wrong.

If you are comfortable with PHP, what you describe is not that much PHP code work.

I'm shooting from the hip, but perhaps a strategy like this may work. I'm sure not all of this will sound as clear as it was in my head! Read and re-read slowly until it makes sense or you have to give up and ask me to explain further. Honestly, though, much more help beyond this, and I'll be writing your app for you--in which case, let me give you my PayPal address.

1. Query all the players and store into an associative array where the player id is the key.

2. Add an 'emailbody' item to each player array. So you'd end up with an array that looks something like this:

[1] {
[name] => John
[surname] => Doe
[email] => john@doe.com
[emailbody] =>
}
[2] {
[name] => Bob
[surname] => Builder
[email] => bob@builder.com
[emailbody] =>
}

3. Query all the newly added events. Loop over them and build a single email message appropriate to let players know about each new event.

4. Loop over all the players in the player array, and set [emailbody] equal to the new events message you created in step 3.

5. Query all upcoming events along with who is signed up for each ordered by player. As you loop over these events, you'll need to keep track of when the player id changes. As long as you are on the same player id, keep appending to an email message. When the player id changes, you can append the message you just built to the [emailbody] in the player array for that player.

6. In the end, you'll have a player array where some or all have a final [emailbody] ready to be sent. Simply loop over the player array and send each email.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the MySQL Forum


Views: 3661 | Replies: 13
Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC