CoderZone.org
Pages: 1 « previous     next »
  Print  
Author Topic: Duplicate column name erro  (Read 850 times) Bookmark and Share
Mr. Crane
Jr. Member
*****
Posts: 63



View Profile WWW
« on: Jan 13, 2011, 01:01:01 pm »

Okay, I'm stumped. I'm getting this error from the query below: #1060 - Duplicate column name 'event_min'. What am I doing wrong??

Code:  
Highlight Mode: (MySQL)
  1. SELECT event_id, event_start, event_stop, event_min, event_max
  2.  
  3. SELECT a.event_id event_id, b.id date_id, b.res res, a.event_min, a.event_max, b.res + INTERVAL
  4. a.event_start_imon
  5. MONTH + INTERVAL a.event_start_iday
  6. DAY + INTERVAL a.event_start_ihr HOUR + INTERVAL a.event_start_imin
  7. MINUTE event_start, b.res + INTERVAL a.event_stop_imon
  8. MONTH + INTERVAL a.event_stop_iday
  9. DAY + INTERVAL a.event_stop_ihr HOUR + INTERVAL a.event_stop_imin
  10. MINUTE event_stop, event_min, event_max
  11. FROM trk_calendar a, trk_period b
  12. WHERE a.event_id =90
  13. AND b.id
  14. IN ( 7683, 7684 )
  15. )a
  16. WHERE event_start < '2011-01-31' + INTERVAL 1
  17. DAY AND event_stop > '2011-01-01' - INTERVAL 1
  18. DAY AND event_start >= event_min
  19. AND event_stop <= event_max
 
Logged

Traveling through the Digital Age at 33⅓rd revolutions per minute.
UnrealEd
Newbie
*
Posts: 22



View Profile
« Reply #1 on: Jan 16, 2011, 03:33:11 am »

I think the error is caused by the subquery: in it you select the field a.event_min, but also the field event_min. However, this shouldn't throw an error as the subquery should return a table with an a.event_min column and an event_min column.

The same goes for the event_max field.

If that isn't causing the error, then I have no idea what is
Logged
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« Reply #2 on: Jan 16, 2011, 09:45:47 pm »

Yep, I gave up on it. This was some SQL written by a friend who's no longer around. I went and rewrote the whole mess in about 1/5 as many lines and replaced the whole thing. I have NO idea why he made it so damn complicated.

You should see some of the SQL this guy wrote...it's super odd and convoluted...he does a complex query, then uses that result in a variable as part of another query, and so on. And not a single line is commented. Here's an example, this is one of his simpler functions below. Caution: looking at this may make your eyes bleed.

Code:  
Highlight Mode: (MySQL)
  1.    $event_start = calculate_date('b.res','a.event_start_imon','a.event_start_iday','a.event_start_ihr','a.event_start_imin');
  2.    $event_stop  = calculate_date('b.res','a.event_stop_imon','a.event_stop_iday','a.event_stop_ihr','a.event_stop_imin');
  3.  
  4.    $qi = 0;
  5.    $qs0 = array();
  6.    foreach ($events as $event_id => $event) {
  7.  
  8.        $start    = $event['start'];
  9.        $stop     = $event['stop'];
  10.        $date_ids = $event['ids'];
  11.  
  12.  
  13.        $date_id_list = join(',',$date_ids);
  14.        $qs0[$qi++] = "
  15.            select a.event_id event_id, b.id date_id,b.res res, a.event_min, a.event_max
  16.            , $event_start event_start
  17.            , $event_stop  event_stop
  18.            , event_min
  19.            , event_max
  20.            from  pdc_calendar a, pdc_period b
  21.            where a.event_id = $event_id
  22.            and b.id in ($date_id_list)
  23.        ";
  24.    }
  25.  
  26.    $qs1 = join("\nunion\n",$qs0);
  27.  
  28. #D print "<pre>\nget_month_events:\n$qs1\n</pre>\n";
  29.  
  30.    $qs2 = "
  31.        select event_id, event_start, event_stop, event_min, event_max from (
  32.            $qs1
  33.        ) a
  34.        where event_start < '$year-$month-31' + interval 1 day
  35.        and event_stop > '$year-$month-01' - interval 1 day
  36.        and event_start >= event_min
  37.        and event_stop  <= event_max
  38.    ";
  39.  
  40.    $qr1 = mysql_query($qs2);
  41.  
  42. #D print "<pre>\n$qs2\n</pre>\n";
  43.  
  44.    if (count($qs0) == 0) {
  45.        return $rtn;
  46.    }
  47.  
  48.    # printf("min(%s) max(%s)\n",$start,$stop);
  49.    $i = 0;
  50.    $qa3 = array();
  51.    while ($row = mysql_fetch_assoc($qr1)) {
  52.        $qa3[$i++] = sprintf("
  53.            select a.res, extract(day from a.res) event_day, '%s' event_start, '%s' event_stop
  54.            , b.is_public, b.event_title, b.event_type, b.event_location, b.event_text, b.event_flags
  55.            , b.form_data, b.hr_dates, b.pplan_event
  56.            , b.event_id, b.event_color
  57.            from pdc_period a, pdc_calendar b
  58.            where a.res between '%s' and '%s'
  59.            and event_id = %d
  60.        ",$row['event_start'],$row['event_stop'],$row['event_start'],$row['event_stop'],$row['event_id']
  61.        );
  62.    }
  63.  
  64.    if (count($qa3) == 0) {
  65.        return $rtn;
  66.    }
  67.  
  68.    $qs3 = sprintf("select * from (%s) a where res between '$year-$month-01' and '$year-$month-31'\n",join("\nunion\n",$qa3));
  69.  
  70. #D print "\n<pre>\n$qs3\n</pre>\n>";
  71.  
  72.    $qr3 = mysql_query($qs3);
  73.  
  74.    while ($row = mysql_fetch_assoc($qr3)) {
  75.        if (! is_array($rtn[$row['event_day']])) {
  76.            $rtn[$row['event_day']] = array();
  77.        }
 
Logged
UnrealEd
Newbie
*
Posts: 22



View Profile
« Reply #3 on: Jan 17, 2011, 07:00:44 am »

Crazyness!

I totally lost it, when I saw there was more to come after this line:
Code:  
Highlight Mode: (PHP)
  1. $qs1 = join("\nunion\n",$qs0);
 
I mean, come on, a union of queries like the one Mr. Crane posted. You have to be a complete idiot or a genius (and we all know there's a small border between Tongue) to write such queries
Logged
Max
Jr. Member
*****
Posts: 75



View Profile WWW
« Reply #4 on: Jan 17, 2011, 07:34:03 am »

I totally lost it, when I saw there was more to come after this line:
Code:  
Highlight Mode: (PHP)
  1. $qs1 = join("\nunion\n",$qs0);
 
Lol, I asked MC for help on this first (the code he posted above is part of the same thing) and he was baffled too. His actual comment was "Holy ****!" Then we got to the second part and it was Tylenol Time. Convoluted code is one thing, a complete lack of comments is another.

Concatenating a query like that is...creative. But there's "creative" and there's "good", and the two don't always coincide.

FYI, the guy that wrote the original code (both of those blobs of madness) is a long-time perl hacker who, the last we heard, is working at Amazon.
Logged
Tags:
Pages: 1
  Print  
 
Jump to: