How do I get the results and count the results in one query? (Im using PDO statements)
SELECT * FROM table; // gets results
SELECT COUNT(*) FROM table; // counts results
$result = mysql_query( "SELECT * FROM table");
$count = mysql_num_rows( $result);
Using PDO:
$statement = $dbh->prepare('SELECT * FROM table');
$statement->execute();
$count = $statement->rowCount();
This will put the record count at the end of each row.
SELECT *
, COUNT(1) OVER () AS RecordCount
FROM table;
SELECT *, (select count(*) FROM table) ct FROM table
you should execute only one query...
$sql = SELECT * FROM table;
$res = mysql_query($sql);
you can have total count by mysql_num_rows(); function.. like this ..
$count = mysql_num_rows($res);
Related
I have to write the following query in zend-framework2
select count(*) from
(
select * from table1
UNION
select * from table2
)
as a
where col1 = condition1 and col2 = condition2;
I have done union of two tables using -
$select1 = new Select('table1');
$select2 = new Select("table2");
$select1->combine($select2);
I don't know how to give an alias after doing the union of two tables and how to get the count of data.
After $select1->combine($select2); -
$sql = new Sql($this->tableGateway->adapter);
$select_string = $sql->getSqlStringForSqlObject($select1);
$sql_string = 'SELECT * FROM (' . $select_string . ') AS select_union WHERE col1 = condition1 and col2 = condition2';
$statement = $this->tableGateway->adapter->createStatement($sql_string);
$resultSet = $statement->execute();
$total_records = count($resultSet);
$resultSet gives data.
$total_records gives total no. of records.
Why isnt this working?
mysql_select_db('a2943462_Pages');
$num_rows = mysql_query("SELECT COUNT(*) FROM PagesInfo");
echo $num_rows;
mysql_query("INSERT INTO `a2943462_Pages`.`PagesInfo`(`ID`, `Title`, `Video`, `Posted`) VALUES ('".$num_rows."', '".$Title."', '".$Embed."', '".$name."')");
printf("Last inserted record has id %d\n", mysql_insert_id());
Because it now echoes:
Resource id #9
but i tought it would echo 4 instead because i have 3 rows inside the Table
Change this:
$num_rows = mysql_query("SELECT COUNT(*) FROM PagesInfo");
To this:
$num_rows = mysql_result(mysql_query("SELECT COUNT(*) FROM PagesInfo"), 0);
This is the PHP method:
$sql="SELECT * FROM pages_info";
$result = mysql_query($sql, $con) or die (echo "$sql" );
$rows = mysql_num_rows($result);
you should do two queries:
first query what you want to show:
SELECT SQL_CALC_FOUND_ROWS * FROM PagesInfo
and second query to get number rows returned:
SELECT FOUND_ROWS()
The in your insert the number of rows returned
The query in php is
$rows = mysql_query("SELECT SQL_CALC_FOUND_ROWS * FROM PagesInfo");
$rs = mysql_query("SELECT FOUND_ROWS()");
$r = mysql_fetch_row($rs);
mysql_free_result($rs);
$row_num = $r[0];
I have this code:
$sql = "SELECT FOUND_ROWS() AS totalRows, COUNT(*) AS totalRefunds FROM table WHERE result = 'refunded'";
$totalRows = $conn->query( $sql )->fetch();
$totalRefunds = $conn->query( $sql )->fetch();
$conn = null;
return ( array ( "results" => $list, "totalRows" => $totalRows[0], "totalRefunds" => $totalRefunds[0] ) );
I want totalRows = 7 and totalRefunds = 1 but the above returns 0 for both. If I remove either the FOUND_ROWS() or COUNT(*) statements then the other one works. I'm guessing there's something wrong in SELECT but not sure what it is. Or maybe something else is wrong???
Thanks in advance.
To obtain this row count, include a SQL_CALC_FOUND_ROWS option in the SELECT statement, and then invoke FOUND_ROWS() afterward
$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM table WHERE result = 'refunded'";
and then SELECT FOUND_ROWS()
Try this way. Assuming you run the query with SQL_CALC_FOUND_ROWS prior to run this
SELECT (#variable := FOUND_ROWS()) AS totalRows, COUNT(*) AS totalRefunds
FROM table WHERE result = 'refunded'
FOUND_ROWS() is used for counting rows as if LIMIT was ignored. It will not append total rows to other query result (unless you select them all, but there's no point if you only need count). Do it simple if you want this information only:
$sql = "SELECT COUNT(*) as totalRows FROM table";
$totalRows = $conn->query( $sql )->fetch();
$sql = "SELECT COUNT(*) as totalRefunds FROM table WHERE result = 'refunded'";
$totalRefunds = $conn->query( $sql )->fetch();
Other way (but different context) is to split counts into all 'result' column types (refunded/not refunded/other..) using GROUP BY so the the total rows can be obtained as sum of single counts returned.
hello can i merge those 2 queries in one query my first query get the number of articles in database and second query get the sum of all visits of all article whats the best method to make it one query
$stmt = $db->query('SELECT * FROM stories');
$story_count = $stmt->rowCount();
$stmt = $db->query("SELECT sum(visits) FROM stories");
$total_visits = $stmt->fetchColumn();
Try like
$stmt = $db->query('SELECT COUNT(*) as total_cnt,
SUM(visits) as total_visits FROM stories');
then excute your query,you will get result from "total_cnt" and "total_visits"
SELECT COUNT(*) as total, SUM(visits) as total_visits FROM stories;
SELECT Story.*, COUNT(*) as total, SUM(Story.visits) as total_visits FROM stories AS Story;
If you want to get other fields along with SUM and COUNT use .*.
Yes try this:
$stmt = $db->query('SELECT count(*),sum(visits) FROM stories');
$result = $stmt->fetch_array(MYSQLI_NUM);
$story_count = $result[0];
$total_visits = $result[1];
I want to use the result set of a query in the where statement of a subsequent query.
Running into a problem because the first query returns multiple results and I don't know how to convert these to a comma-separated string for use in an 'IN' () statement.
$Result1 = mysql_query ("
select id from Table1
where
upper('$Input') = Employee_number
")
or die(mysql_error());
$Result2 = mysql_query ("
Select * from table2 where ID in ($Result1)")
You can combine this into a single MySQL statement like this:
SELECT * FROM Table2 WHERE id IN
(SELECT id FROM Table1 where upper('$Input') = Employee_number);
But if you really want to do it like in your question, you will probably have to lookup how to work with the results from mysql_query, just look that up in a function reference.
$query1 = mysql_query("
select id from Table1
where upper('$Input') = Employee_number
") or die(mysql_error());
$ids = array();
while($Result1 = mysql_fetch_array($query1))
{
$ids[] = $Result1['id'];
}
if(sizeof($ids))
{
$n_ids = implode(',', $ids);
$query2 = mysql_query("
select * from Table2
where ID IN (" . $n_ids . ")
") or die(mysql_error());
}