Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6597055
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:09:32+00:00 2026-05-25T18:09:32+00:00

I have the following table. ——————————————— check_id | action_id | user_id | dt |

  • 0

I have the following table.

---------------------------------------------
check_id | action_id | user_id |         dt |
---------------------------------------------
       1 |         1 |       6 | 2011-09-17 |
       2 |         1 |       6 | 2011-09-18 |
       3 |         3 |       6 | 2011-09-19 |
       4 |         3 |       6 | 2011-09-20 |
---------------------------------------------

I would like to query this table and get the following result.

-----------------------------------------------
action_id | user_id |   dt_start |     dt_end |
-----------------------------------------------
       1 |        6 | 2011-09-17 | 2011-09-18 |
       3 |        6 | 2011-09-19 | 2011-09-20 |
-----------------------------------------------

So I’m using the following query.

$checks->select()
->from(array('c1' => 'checks'), array('dt as dt_start')
->joinLeft(array('c2' => 'checks'), 'c1.action_id = c2.action_id', array('dt as dt_end')
->where('c1.user_id = ?', $userId)
->group('c1.action_id')

But this gives me the following result.

-----------------------------------------------
action_id | user_id |   dt_start |     dt_end |
-----------------------------------------------
       1 |        1 | 2011-09-17 | 2011-09-17 |
       1 |        3 | 2011-09-19 | 2011-09-19 |
-----------------------------------------------

Can someone tell me what I am doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:09:33+00:00Added an answer on May 25, 2026 at 6:09 pm

    Try this query, without the group by:

    SELECT *
    FROM checks c1 LEFT OUTER JOIN checks c2 ON c1.action_id = c2.action_id 
    WHERE c1.user_d = 6
    

    You’ll see that you get matches where c1.dt < c2.dt, which is what you want.
    But you also get matches where c1.dt > c2.dt, and where c1.dt = c2.dt.
    That is, the self-join includes results where c1 and c2 point the very same row.

    Then you use a GROUP BY that collapses multiple rows into one, but MySQL chooses an arbitrary row from the group. (This is an ambiguous query, and if you SET SQL_MODE='ONLY_FULL_GROUP_BY' you’d get an error.)

    So your self-join and GROUP BY only returns c1 and c2 that are actually the very same row.

    To fix this, you should add a condition that c1.dt < c2.dt.

    $checks->select()
    ->from(array('c1' => 'checks'), array('dt as dt_start')
    ->joinLeft(array('c2' => 'checks'), 
               'c1.action_id = c2.action_id AND c1.dt < c2.dt', 
               array('dt as dt_end')
    ->where('c1.user_id = ?', $userId)
    

    You probably don’t need the GROUP BY at all in that case, assuming that you don’t have multiple starts and ends for each action.

    By the way, this type of complexity is one reason that it’s usually recommended to store event start/end data in a single row, with the start in one column and the end in a second column.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following table it shows like this: ABC A B C 123 Hello
I have the following table in MSSQL2005 id | business_key | result 1 |
I have the following table: Year Line January Febraury March .... December 2011 B1
I have the following table structure with data as ReadingDate Unit 01-05-2011 10 01-06-2011
I have following table: res_id quiz_id sender_id user_id question_id points 1 1 2 33
I have following table: FOLDER[ id int, name varchar2(10), parent_folder_id int ] I would
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have the following table relationship in my database: Parent / \ Child1 Child2
I have the following table schema; CREATE TABLE `db1`.`sms_queue` ( `Id` INTEGER UNSIGNED NOT
I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.