Note that there are some explanatory texts on larger screens.

plurals
  1. POSELECT inside SELECT statement returns 0 count
    primarykey
    data
    text
    <p>I have this calendar where there is a list of events in every day and I use this query to count the events in a particular date. In my database I have <code>start_time</code> and <code>end_time</code> field where the user have a schedule of materials to be specific. I tried to <code>select</code> the events in that day but it seems there's something wrong in my query. Because the user can borrow more than one material so it will be stored in the database with the same <code>start_time</code> and <code>end_time</code> as well. What I want is to count all the data of the user with the same <code>start_time</code> and I tried <code>GROUP BY</code> but it seems doesn't work either. Here is my database:</p> <pre><code> ----Table: schedule---- id | materialID | borrowerID | date_reserve | start_time | end_time| ----------------------------------------------------------------------------------- 9 | 7 | bobi | 2013-08-16 | 07:01:12 | 07:01:12| 10 | 10 | bobi | 2013-08-16 | 07:01:12 | 07:01:12| 11 | 12 | bobi | 2013-08-16 | 07:01:12 | 07:01:12| 12 | 7 | sobi | 2013-08-18 | 07:01:12 | 07:01:12| ------------------------------------------------------------------------------------ </code></pre> <p>Here is my query:</p> <pre><code> $cal_data = array(); for($i=1;$i&lt;=31;$i++) { $date = "$year-$month-$i"; $this-&gt;db-&gt;select('(SELECT COUNT(id) as count_s FROM schedule WHERE date_reserve='.$date.' GROUP BY start_time) as count', FALSE); $this-&gt;db-&gt;select('date_reserve,borrowerID,start_time,end_time'); $this-&gt;db-&gt;from('schedule'); $this-&gt;db-&gt;where('date_reserve',"$year-$month-$i"); $this-&gt;db-&gt;group_by('start_time'); $query = $this-&gt;db-&gt;get(); foreach ($query-&gt;result() as $row) { $cal_data[$i] = ($row-&gt;count &gt; 0) ? $row-&gt;count.' ' .'event(s)' : ''; } } </code></pre> <p>So the <strong>expected</strong> output with the will be: </p> <pre><code> count | date_reserve | borrowerID | start_time | end_time --------------------------------------------------------------------------------------------------- 1 | 2013-08-16 | bobi | 07:01:12 | 07:01:12 </code></pre> <p>In here there's a big <strong>BUT</strong> where in that query it will give you this output. Note: <code>I'm using CodeIgniter</code>.<br> I used <code>$this-&gt;output-&gt;enable_profiler(TRUE);</code> and try the date <code>2013-08-16</code>(because it is multiple selection) to MySQL on my server and give me this.</p> <pre><code> count | date_reserve | borrowerID | start_time | end_time --------------------------------------------------------------------------------------------------- NULL | 2013-08-16 | bobi | 07:01:12 | 07:01:12 </code></pre> <p>So what do you think the solution for this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload