MySQL Limit 2 How to select second row - mysql

$sMonOperationHours = "SELECT * FROM ".COMPANY_OPERATION_HOURS_TBL." WHERE
f_company_id = '$g_companyid' AND f_day = 'MON' LIMIT 2";
$rsMonOperationHours = mysql_query($sMonOperationHours);
$rowMonOperationHours = mysql_fetch_array($rsMonOperationHours);
$iMonStartTime = $rowMonOperationHours['f_start_time'];
$iMonStartTime2 = ???;
$iMonEndTime = $rowMonOperationHours['f_end_time'];
$iMonEndTime2 = ???;
After limiting my SQL query result to 2, how do I select the f_start_time and f_end_time for the second row?

You can use in LIMIT and OFFSET for getting the second result
$sMonOperationHours = "SELECT * FROM ".COMPANY_OPERATION_HOURS_TBL." WHERE
f_company_id = '$g_companyid' AND f_day = 'MON' LIMIT 1 OFFSET 1";
$rsMonOperationHours = mysql_query($sMonOperationHours);
$rowMonOperationHours = mysql_fetch_array($rsMonOperationHours);
$iMonStartTime = $rowMonOperationHours['f_start_time'];
$iMonEndTime = $rowMonOperationHours['f_end_time'];
Another way to do this, is if you want both of results, that is the way:
$sMonOperationHours = "SELECT * FROM ".COMPANY_OPERATION_HOURS_TBL." WHERE
f_company_id = '$g_companyid' AND f_day = 'MON' LIMIT 2";
$rsMonOperationHours = mysql_query($sMonOperationHours);
$rowsData = array();
$i = 0;
while ($row = mysql_fetch_array($rsMonOperationHours)) {
$rowsData[$i] = array();
$rowsData[$i][] = $row['f_start_time'];
$rowsData[$i][] = $row['f_end_time'];
$i++;
}
All data will be saved in rowsData which contains the results in array.

Related

How to use loop for following Laravel code?

$query1 = User::where('sponser_id',$_SESSION['ruserid'])->get('userid');
$count1= count($query1);
$query2 = User::whereIn('sponser_id',$query1)->get('userid');
$count2= count($query2);
$query3 = User::whereIn('sponser_id',$query2)->get('userid');
$count3= count($query3);
$query4 = User::whereIn('sponser_id',$query3)->get('userid');
$count4= count($query4);
$query5 = User::whereIn('sponser_id',$query4)->get('userid');
$count5= count($query5);
$query6 = User::whereIn('sponser_id',$query5)->get('userid');
$count6= count($query6);
$query7 = User::whereIn('sponser_id',$query6)->get('userid');
$count7= count($query7);
$allcount=$count1+$count2+$count3+$count4+$count5+$count6+$count7;
I want to calculate the total downlines of users. This code works fine. But How to simplifies this code using a loop?
$allcount = 0;
$query1 = User::where('sponser_id',$_SESSION['ruserid'])->get('userid');
$count1 = count($query1);
$allcount = $allcount + $count1;
for ($i=2; $i<8; $i++) {
if ($i>2){
$j = $i-1;
${"query".$j} = User::whereIn('sponser_id',${"query".($j-1)})->get('userid');
}
${"query".$i} = User::whereIn('sponser_id',${"query".($i-1)})->get('userid');
${"count".$i} = count(${"query".$i});
$allcount = $allcount + ${"count".$i};
}
return $allcount;

Get row counts from a multiple

I'm executing a multi-delete DELETE query, like so:
$query = "DELETE FROM foo WHERE 1 = 1; DELETE FROM bar WHERE 1 = 1";
$statement = $this->getEntityManager()->getConnection()->prepare($query);
$statement->execute();
I'm aware that I can use $statement->getRowCount() if my query contained a single delete, but how can I both row counts?
AFAIK there is no way doing this in doctrine, but another solution could be:
$queries = [
"DELETE FROM foo WHERE 1 = 1;",
"DELETE FROM bar WHERE 1 = 1;"
];
$connection = $this->getEntityManager()->getConnection();
$affectedRows = 0;
foreach($queries as $query)
{
$statement = $connection->prepare($query);
$statement->execute();
$affectedRows = $affectedRows + $statement->getRowCount();
}

Dynamic mysql query LIMIT not working

I'm creating the parameters to LIMIT a mysql query using PHP variables. I'm outputting the query to make sure it's right, and it is. However the results completely ignore the offset parameter. However if I manually type in the LIMIT, it works fine.
$page_rows = 5;
$max = ($pagenum - 1) * $page_rows .',' .$page_rows;
$qry = "SELECT * FROM ".$wpdb->prefix."nslikethis_votes WHERE user_id = '$uid' AND active = 1 ORDER BY time DESC LIMIT " . $max;
$rs = mysql_query($qry);
$result = array();
if($rs && $rows>0){
while($lc = mysql_fetch_object($rs, "LikedContent")){
$result[] = $lc;
}
}
return $result;
Outputting $qry gives me this whether I use $max or manually enter '5,5':
SELECT * FROM wp_nslikethis_votes WHERE user_id = '1' AND active = 1 ORDER BY time DESC LIMIT 5,5
Try doing this for check:
$page_rows = "5";
$max = ($pagenum - 1) * $page_rows .',' .$page_rows;
$qry = "SELECT * FROM ".$wpdb->prefix."nslikethis_votes WHERE user_id = '$uid' AND `active` = 1 ORDER BY `time` DESC LIMIT " . $max;
//check for variables in query like $wpdb->prefix, $uid: are they fine, and try adding tilded sign as above
Hope this works for you

get me the latest Change from Select Query in below given condition

I have a Table structure as
id, trackid, table_name, operation,
oldvalue, newvalue, field,
changedonetime
Now if I have 3 rows for the same "trackid" same "field", then how can i select the latest out of the three?
i.e. for e.g.:
id = 100 trackid = 152 table_name
= jos_menu operation= UPDATE oldvalue = IPL newvalue = IPLcccc
field = name live = 0 changedonetime =
2010-04-30 17:54:39
and
id = 101 trackid = 152 table_name =
jos_menu operation= UPDATE oldvalue
= IPLcccc newvalue = IPL2222 field = name live = 0 changedonetime =
2010-04-30 18:54:39
As u can see above the secind entry is the latest change,
Now what query I should use to get the only one and Latest row out of many such rows...
$distupdqry = "select DISTINCT trackid,table_name from jos_audittrail where live = 0 AND operation = 'UPDATE'";
$disupdsel = mysql_query($distupdqry);
$t_ids = array();
$t_table = array();
while($row3 = mysql_fetch_array($disupdsel))
{
$t_ids[] = $row3['trackid'];
$t_table[] = $row3['table_name'];
//$t_table[] = $row3['table_name'];
}
//echo "<pre>";print_r($t_table);echo "<pre>";
//exit;
for($n=0;$n<count($t_ids);$n++)
{
$qupd = "SELECT * FROM jos_audittrail WHERE operation = 'UPDATE' AND trackid=$t_ids[$n] order by changedone DESC ";
$seletupdaudit = mysql_query($qupd);
$row4 = array();
$audit3 = array();
while($row4 = mysql_fetch_array($seletupdaudit))
{
$audit3[] = $row4;
}
$updatefield = '';
for($j=0;$j<count($audit3);$j++)
{
if($j == 0)
{
if($audit3[$j]['operation'] == "UPDATE")
{
//$insqry .= $audit2[$i]['operation']." ";
//echo "<br>";
$updatefield .= "UPDATE `".$audit3[$j]['table_name']."` SET ";
}
}
if($audit3[$j]['operation'] == "UPDATE")
{
$updatefield .= $audit3[$j]['field']." = '".$audit3[$j]['newvalue']."', ";
}
}
/*echo "<pre>";
print_r($audit3);
exit;*/
$primarykey = "SHOW INDEXES FROM `".$t_table[$n]."` WHERE Key_name = 'PRIMARY'";
$prime = mysql_query($primarykey);
$pkey = mysql_fetch_array($prime);
$updatefield .= "]";
echo $updatefield = str_replace(", ]"," WHERE ".$pkey['Column_name']." = '".$t_ids[$n]."'",$updatefield);
}
In the above code I am fetching ou the distinct IDs in which update operation has been done, and then accordingly query is fired to get all the changes done on different fields of the selected distinct ids...
Here I am creating the Update query by fetching the records from the initially described table which is here mentioned as audittrail table...
Therefore I need the last made change in the field so that only latest change can be selected in the select queries i have used...
please go through the code.. and see how can i make the required change i need finally..
This is another question of the greatest-n-per-group category, which comes up several times per week on Stack Overflow.
Here's how I'd solve it in your case:
SELECT j1.*
FROM jos_audittrail j1 LEFT OUTER JOIN jos_audittrail j2
ON (j1.trackid = j2.trackid AND j1.field = j2.field
AND j1.changedonetime < j2.changedonetime)
WHERE j2.id IS NULL;

How to select latest change done in the given Table structure?

I have a Table structure as
id, trackid, table_name, operation, oldvalue, newvalue, field, changedonetime
Now if I have 3 rows for the same "trackid" same "field", then how can i select the latest out of the three?
i.e. for e.g.:
id = 100 trackid = 152 table_name
= jos_menu operation= UPDATE oldvalue = IPL newvalue = IPLcccc
field = name live = 0 changedonetime =
2010-04-30 17:54:39
and
id = 101 trackid = 152 table_name =
jos_menu operation= UPDATE oldvalue
= IPLcccc newvalue = IPL2222 field = name live = 0 changedonetime =
2010-04-30 18:54:39
As u can see above the secind entry is the latest change,
Now what query I shoud use to get the only one and Latest row out of many such rows...
UPDATED
$distupdqry = "select DISTINCT trackid,table_name from jos_audittrail where live = 0 AND operation = 'UPDATE'";
$disupdsel = mysql_query($distupdqry);
$t_ids = array();
$t_table = array();
while($row3 = mysql_fetch_array($disupdsel))
{
$t_ids[] = $row3['trackid'];
$t_table[] = $row3['table_name'];
//$t_table[] = $row3['table_name'];
}
//echo "<pre>";print_r($t_table);echo "<pre>";
//exit;
for($n=0;$n<count($t_ids);$n++)
{
$qupd = "SELECT * FROM jos_audittrail WHERE operation = 'UPDATE' AND trackid=$t_ids[$n] order by changedone DESC ";
$seletupdaudit = mysql_query($qupd);
$row4 = array();
$audit3 = array();
while($row4 = mysql_fetch_array($seletupdaudit))
{
$audit3[] = $row4;
}
$updatefield = '';
for($j=0;$j<count($audit3);$j++)
{
if($j == 0)
{
if($audit3[$j]['operation'] == "UPDATE")
{
//$insqry .= $audit2[$i]['operation']." ";
//echo "<br>";
$updatefield .= "UPDATE `".$audit3[$j]['table_name']."` SET ";
}
}
if($audit3[$j]['operation'] == "UPDATE")
{
$updatefield .= $audit3[$j]['field']." = '".$audit3[$j]['newvalue']."', ";
}
}
/*echo "<pre>";
print_r($audit3);
exit;*/
$primarykey = "SHOW INDEXES FROM `".$t_table[$n]."` WHERE Key_name = 'PRIMARY'";
$prime = mysql_query($primarykey);
$pkey = mysql_fetch_array($prime);
$updatefield .= "]";
echo $updatefield = str_replace(", ]"," WHERE ".$pkey['Column_name']." = '".$t_ids[$n]."'",$updatefield);
}
Here I am creating the Update query by fetching the records from the initially described table which is here mentioned as audittrail table... please go throught the code.. and see how can i make the required change i need finally..
select * from TableName order by id DESC LIMIT 1
EDITED. YEs you can if you want to order by "changedonetime" USE.
select * from TableName order by changedonetime DESC LIMIT 1