Solely COUNT(*) from Zend_DB Select Statement (Subquery) - php

I'm trying to wrap a count(*) query around an existing Zend_Db select statement, but all I was able to get is:
SELECT `t`.*, COUNT(*) AS `TotalRecords` FROM (SELECT ....) AS `t`
However I like to get rid of the t.* as I only need the count(*).
This is my code so far:
$db = Zend_Registry::get('db');
$select = $dbmodel->getSomething(); //zend select object
$outterSelect = new Zend_Db_Select($db);
$outterSelect->from($select)->columns(array('TotalRecords' => new Zend_Db_Expr('COUNT(*)')));
echo $outterSelect->__toString();
Any help is appreciated!

You can simply write:
$outterSelect->from($select, 'COUNT(*) as TotalRecords');

Related

Why is this Union Query not working

I am trying to access multiple tables from mySQL via my php in wordpress. My code looks like this...
function retrieve_libraries( $data ) {
$second_db = new wpdb(DB_USER, DB_PASSWORD, "saic3_LibraryIndex", DB_HOST);
$query = "SELECT * FROM `Library` WHERE library_id=74 UNION SELECT * FROM `Library_Collection` WHERE library_id=74";
$list = $second_db->get_results($query);
return $list;
}
Before I had my query like this...
$query = "SELECT * FROM `Library` WHERE library_id=74"
And the data returned just fine. I have been looking into the UNION statement and the UNION ALL and both seem to not work. Am I calling this wrong? is my syntax off? Asking for a friend.
UNION isn't going to work unless Library and Library_Collection have the same number and type of columns.
Based on the table names*, it looks like you probably want to JOIN the two tables instead of UNION them.
$query = "SELECT * FROM `Library`
LEFT JOIN `Library_Collection`
ON Library.library_id = Library_Collection.library_id
WHERE Library.library_id=74";
*(I'm assuming Library_Collection is the things a Library has.)

Codeigniter SQL query build

I'm having trouble to convert the below SQL query into CodeIgniter query builder:
$row = $db->prepare("SELECT DATE(message_timestamp) Date, COUNT(DISTINCT messageid) totalCount FROM ic_deliveryreceipts GROUP BY DATE(message_timestamp)");
This is the latest I've tried:
$this->db->select(DATE(message_timestamp) as 'Date', COUNT(DISTINCT messageid) as 'totalCount');
$this->db->group_by(DATE(message_timestamp));
$query = $this->db->get($this->ic_receipts);
Any help more than welcome!
Thank you
$row = $this->db
->query("SELECT DATE(message_timestamp) Date,
COUNT(DISTINCT messageid) totalCount
FROM ic_deliveryreceipts
GROUP BY DATE(message_timestamp)")
->row();
$row is a stdClass object with two properties: Date, totalCount
echo $row->Date . " " . $row->totalCount;
I think you need quotes, and also put false on the second (optional) parameter in $this->db->select, as seen in codeigniter docs:
$this->db->select() accepts an optional second parameter. If you set
it to FALSE, CodeIgniter will not try to protect your field or table
names. This is useful if you need a compound select statement where
automatic escaping of fields may break them.
Try writing like this:
$this->db->select("DATE(message_timestamp) as 'Date',
COUNT(DISTINCT messageid) as 'totalCount'"
, FALSE);
$this->db->group_by("DATE(message_timestamp)");
$query = $this->db->get($this->ic_receipts);
This is what I finally come up with:
$this->db->select('DATE(message_timestamp) Date, COUNT(DISTINCT messageid) totalCount');
$this->db->from('ic_deliveryreceipts');
$this->db->group_by('DATE(message_timestamp)');
$query = $this->db->get();
Thank you for your suggestions

PHP Prepared Statement variable binding error with subquery

I have a query with a few subqueries like so
SELECT ...
FROM (SELECT ...
FROM ...
GROUP BY ...) as speedLimitCalc INNER JOIN
(SELECT ...
FROM date a INNER JOIN HOURLY_TEST b ON a.[FULL_DAY_DT] = b.DATE
WHERE (b.DATE BETWEEN '".$date_s."' AND '".$date_e."')
AND HOUR BETWEEN ".$time_s." AND ".$time_e."
AND(LKNO BETWEEN '".$lkno_s."' and '".$lkno_e."')
AND RDNO= '".$rdno."'
AND pub_hol IN (".$pubholquery.")
AND school_hol IN (".$schholquery.")
AND day_no IN (".$dayquery.")
GROUP BY RDNO, LKNO, PRESCRIBED_DIRECTION, CWAY_CODE) as origtable ON ...
,(SELECT ...
FROM [Dim_date]
WHERE (FULL_DAY_DT BETWEEN '".$date_s."' AND '".$date_e."')
AND pub_hol IN (".$pubholquery.")
AND school_hol IN (".$schholquery.")
AND day_no IN (".$dayquery.") ) as c
ORDER BY ...
where I am inserting variables in the inner query where clause.
I am trying to parametrize this query using odbc_prepare and odbc_execute, however I am running into issues of binding the variables. At present, when I use the following
$result = odbc_prepare($connection, $query);
odbc_execute($result)or die(odbc_error($connection));
to run this query, everything works fine. However, when I try to bind a variable, such as
AND RDNO= ?
...
odbc_execute($result, array($rdno))or die(odbc_error($connection));
I get the following error message.
PHP Warning: odbc_execute() [/phpmanual/function.odbc-execute.html]: SQL error: [Microsoft][ODBC SQL Server Driver]Invalid parameter number, SQL state S1093 in SQLDescribeParameter
My guess is that it's because I'm binding a variable in a subquery, since this procedure works when the Where clause is in the top Select query.
I was wondering whether anyone else has encountered this issue before, and how they solved it? Thanks
Fixed the issue by removing the need for parameters in the subquery by separating the query into multiple queries using temporary tables.
$query = "SELECT ...
INTO ##avgspeedperlink
FROM Date a INNER JOIN HOURLY_TEST ON a.[FULL_DAY_DT] = b.DATE
WHERE (b.DATE BETWEEN ? AND ?)
AND HOUR BETWEEN ? AND ?
AND(LKNO BETWEEN ? and ?)
AND RDNO= ?
AND pub_hol IN (".$pubholquery.")
AND school_hol IN (".$schholquery.")
AND day_no IN (?,?,?,?,?,?,?)
GROUP BY RDNO, LKNO, PRESCRIBED_DIRECTION, CWAY_CODE";
$result = odbc_prepare($connection, $query);
odbc_execute($result, array($date_s,$date_e,$time_s,$time_e,$lkno_s,$lkno_e,$rdno,$daysanitised[0],$daysanitised[1],$daysanitised[2],$daysanitised[3],$daysanitised[4],$daysanitised[5],$daysanitised[6]))or die(odbc_error($connection));
$query = "SELECT ...
INTO ##daysinperiod
FROM [RISSxplr].[dbo].[Dim_date]
WHERE (FULL_DAY_DT BETWEEN ? AND ?)
AND pub_hol IN (".$pubholquery.")
AND school_hol IN (".$schholquery.")
AND day_no IN (?,?,?,?,?,?,?)";
$result = odbc_prepare($connection, $query);
odbc_execute($result, array($date_s,$date_e,$daysanitised[0],$daysanitised[1],$daysanitised[2],$daysanitised[3],$daysanitised[4],$daysanitised[5],$daysanitised[6]))or die(odbc_error($connection));
$query = "SELECT ...
FROM ##avgspeedperlink, ##daysinperiod
ORDER BY LKNO, OUTBOUND
drop table ##avgspeedperlink
drop table ##daysinperiod";
Note that I had to use double ## for making the temporary tables (single # means that table is local to the query, ## means that the temporary table becomes global for multiple queries).

Sub-queries in a while loop: what is fastest?

I have to check a parameter in a while loop by counting the number of lines in a table. Right now I am doing it this way but it seems very inefficient
while ($stmt->fetch()) {
$stmt3 = $mysqli->prepare("SELECT COUNT(DISTINCT id_roster)
FROM dispo
WHERE id_member = ?");
$stmt3->bind_param('i', $id_alerte);
$stmt3->execute();
$stmt3->store_result();
$stmt3->bind_result($number_dispo);
$stmt3->fetch();
echo $number_dispo;
}
Any idea how I can make it faster (without repeating the query in each loop iteration?
SELECT
id_member, COUNT(DISTINCT dispo.id_roster)
FROM
test.dispo
GROUP BY id_member;
For this data:
id_roster;id_member
1;1
1;1
2;1
3;2
4;2
5;2
6;3
produces a list of all id_members together with associated distinct count of id_roster for each one:
id_member;count
1;2
2;3
3;1
You could further limit this query to only certain id_members
by adding a
WHERE id_member IN (1,2)
clause before the GROUP BY.
You need to combine the outer query $stmt with the inner query $stmt3. You've not provided the code for the outer query so i've tried to demonstrate it generally:
SELECT ot.*, rc.id_roster_count
FROM other_table ot
LEFT JOIN
(
SELECT id_member, COUNT(DISTINCT id_roster) AS id_roster_count
FROM dispo GROUP BY id_member
)
AS rc ON ot.id_member = rc.id_member
WHERE ot.other_value > 5
It may be possible to further optimize this. It depends on your other query and table structure.
You can do it by simply extracting all the data in a single query and use group by to format the data and then use the loop to do the desired option.

sql query from 2 tables and php

I have a simple php code that fetches data from a sql table.
TABLE 1
date_time
name
and another table which store name(same as TABLE1) and picture link.
TABLE 2
name
pic
PHP
$db = new PDO("mysql:host=$host;dbname=$dbname", "$username", "$password");
$sql = "select * from table1";
foreach ($db->query($sql) as $row) {
echo '<span class=name>'.$row['name'].'</span>';
echo "<img src=".$row['pic']." width='25px' height='25px'/>";
$db = null;
}
What i want is to be show the picture of the name from TABLE 2($row['pic']) according to name in TABLE 1.
How the sql statement and the php code should be in order to achieve this?
I run a sql statement in phpMyadmin and got it,but couldnt translate it in php code.
select table1.name,table2.pic from table1,table2 where table1.name=table2.name;
How about using JOIN:
SELECT table1.name, table2.pic
FROM table1
INNER JOIN table2
ON table1.name = table2.name
try this, use as keyword to alias your column names, so that you can get 'name' and 'pic' index in the loop.
select
table1.name as `name`,
table2.pic as `pic`
from table1, table2
where table1.name = table2.name;
a better way to do it use joins
select
`table1`.`name` as `name`,
`table2`.`pic` as `pic`
from `table1`
inner join `table2`
on `table1`.`name` = `table2`.`name`;

Categories