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.
Related
I've got returns results from a query but I would like it to return multiple results, this is the current query:
$qry = "SELECT * FROM properties WHERE isLeased = 0 AND featured = 1 ORDER BY RAND() LIMIT 9";
$res = mysqli_query($mysqli, $qry) or die('-1' . mysqli_error());
What I would like is for it to return those results, as well as the results for:
$qry = "SELECT * FROM properties WHERE isLeased = 0 AND propertyType = 'For Sale' ORDER BY RAND() LIMIT 3";
$res = mysqli_query($mysqli, $qry) or die('-1' . mysqli_error());
But if I just add the second section of code below the first one it only returns the results of the first one.
How could I tell it to return results based on a couple of tables?
You can use a UNION query:
SELECT t1.*
FROM
(
SELECT *
FROM properties
WHERE isLeased = 0 AND featured = 1
ORDER BY RAND()
LIMIT 9
) t1
UNION ALL
SELECT t2.*
FROM
(
SELECT *
FROM properties
WHERE isLeased = 0 AND propertyType = 'For Sale'
ORDER BY RAND()
LIMIT 3
) t2
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.
I'd like to update a row in a MySQL table when a specific row number is reached
This is a little confusing since , the real row number of the record isn't a column in the table.
And there's no question of iterating over rows , since we're not iterating over an array as in mysql_fetch_array()
So , if I'd like to update - say the 3rd row of the table , what would the query be like?
I'm a noob at MySQL
Thanks a ton for your help ! :D
$link = mysqli_connect("localhost", "my_user", "my_password", "my_db");
$query = "SELECT MyColoumn FROM Mytable";
$result = mysqli_query($link, $query);
$row_needed = 3; //Your needed row e.g: 3rd row
for ($i=1,$i=$row_needed,$i++) {
$row = mysqli_fetch_array($result);
}
// now we are in 3rd row
$query = "UPDATE MyColumn FROM MyTable SET MyColumn = '".$MyColumnUpdate."' WHERE MyColumn = '".$row['MyColumn']."' ";
$result = mysqli_query($link, $query);
...
Try this query
UPDATE
tbl a,
(Select
#rn:=#rn+1 as rowId,
tbl.*
from
tbl
join
(select #rn:=0) tmp) b
SET
a.columnName = <VALUE>
WHERE
b.rowId = <rowNumber> AND
a.id = b.id;
NOTE The id column must be a unique one, you can use the primary key of that table...
SQLFIDDLE
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];
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);