tags:

views:

75

answers:

5

Hey guys, I created a list for fixtures.

$result = mysql_query("SELECT date FROM ".TBL_FIXTURES." WHERE compname = '$comp_name' GROUP BY date");
    $i = 1;
    $d = "Start";
    while ($row = mysql_fetch_assoc($result)) 
    {
        $odate = $row['date'];
        $date=date("F j Y", $row['date']);
        echo "<p>Fixture $i - $d to $date</p>";
    }

As you can see from the query, the date is displayed from the fixtures table. The way my system works is that when a fixture is "played", it is removed from this table. Therefore when the entire round of fixtures are complete, there wont be any dates for that round in this table. They will be in another table.

Is there anyway I can run an other query for dates at the same time, and display only dates from the fixtures table if there isnt a date in the results table?

"SELECT * FROM ".TBL_CONF_RESULTS." 
                               WHERE compid = '$_GET[id]' && type2 = '2' ORDER BY date" 

That would be the second query!

EDIT FROM HERE ONWARDS...

Is there anyway I can select the date from two tables and then only use one if there are matches. Then use the rows of dates (GROUPED BY) to populate my query? Is that possible?

+1  A: 

From the notes on this page:

//performs the query
$result = mysql_query(...);

$num_rows = mysql_num_rows($result);

//if query result is empty, returns NULL, otherwise,
//returns an array containing the selected fields and their values
if($num_rows == NULL)
{
    // Do the other query
}
else
{
    // Do your stuff as now
}
ChrisF
I don't think that will work. It will return rows, but not ENOUGH rows. Say I have 4 teams in a league, i have 3 rounds of fixtures. When the first round of fixtures leave the fixtures table, thats when i need the other query to populate the date. But the query will still return rows as the second and third round of fixtures are still there. So I think if i check the results table for a date, if there are rows, compare them with the fixture table. Then only return one row if there are 2 identical rows, otherwise return the one from fixtures.
Luke
@Luke - I must have mis-read your question - however can't you still use the `$num_rows` variable?
ChrisF
Sorry, I find it hard to explain myself sometimes. You must find it even harder to understand me. I am trying to check the results table first for dates, then the fixtures table. If there are duplicates, I only want to use ONE of those dates. Then i want to display the dates USING GROUP BY. How can i do this? I'm so confused.
Luke
+1  A: 
  1. WHERE compid = '$_GET[id]' presents an oportunity for SQL Injection.
  2. Are TBL_FIXTURES and TBL_CONF_RESULTS supposed to read $TBL_FIXTURES and $TBL_CONF_RESULTS?
Dolph
I would imagine that `TBL_FIXTURES` is a `define()` or a `const`
gnarf
Both defined...
Luke
+1  A: 

ChrisF has the solution!

One other thing you might think about is whether it is necessary to do a delete and move to another table. A common way to solve this type of challenge is to include a status field for each record, then rather than just querying for "all" you query for all where status = "x". For example, 1 might be "staging", 2 might be "in use", 3 might be "used" or "archived" In your example, rather than deleting the field and "moving" the record to another table (which would also have to happen in the foreach loop, one would assume) you could simply update the status field to the next status.

So, you'd eliminate the need for an additional table, remove one additional database hit per record, and theoretically improve the performance of your application.

bpeterson76
+1 for suggesting better DB design as the solution :)
gnarf
Ahhh the better database design could be an option infact. However, ChrisF doesnt have the solution because of the following. I need to check the dates from TWO tables. Then I can GROUP the dates and use them. How can i do this? Where i take dates from two tables, if there are duplicates, only use once?
Luke
+1  A: 

Seems like what you want is a UNION query.

$q1 = "SELECT DISTINCT date FROM ".TBL_FIXTURES." WHERE compname = '$comp_name'";
$q2 = "SELECT DISTINCT date FROM ".TBL_CONF_RESULTS. 
      "WHERE compid = '$_GET[id]' && type2 = '2'";

$q = "($q1) UNION DISTINCT ($q2) ORDER BY date";
gnarf
This looks like this would also work. There were two UNION answers and I have just seen this one. Thanks for the help bud.
Luke
+2  A: 

It sounds like you want to UNION the two result sets, akin to the following:

SELECT f.date FROM tbl_fixtures f
WHERE f.compname = '$comp_name'
UNION SELECT r.date FROM tbl_conf_results r
WHERE r.compid = '$_GET[id]' AND r.type2 = '2'
GROUP BY date

This should select f.date and add rows from r.date that aren't already in the result set (at least this is the behaviour with T-SQL). Apparently it may not scale well, but there are many blogs on that (search: UNION T-SQL).

jessicah
This is it. I just didn't know about UNION! I changed GROUP BY to ORDER BY as that was a flaw in my original query. Thankyou so much dude, really really helpful!
Luke
I was wondering about the oddity of that GROUP BY clause ;-)
jessicah