SQL Server get number of rows - php

I have a table called replies and it has a column named topic_id.
What I want to do is count how many rows have the topic_id 1.
I tried
SELECT COUNT(*)
FROM [***].[replies]
WHERE [***].[topic_id] = '1'
But in this case I don't know if this is a valid query. If it is, how can I pull it back into a variable called $num_rows in my php code?
Note: * is actually a prefix I covered.

Something like this should work:
--declare the variable
DECLARE #num_rows INT
--get the total number of rows
SELECT #num_rows = COUNT(*) FROM replies WHERE topic_id = 1
--get a distinct count of rows (if for some reason you need it)
SELECT #num_rows = COUNT(DISTINCT reply_id) FROM replies WHERE topic_id = 1
--return the result set
SELECT #num_rows AS num_rows
If you just want to return a column called num_rows though, this would be a much more efficient approach:
SELECT COUNT(*) AS num_rows FROM replies WHERE topic_id = 1

Related

How to get a response from a mysqli multi query or how do I get the number of the row sorted in descending order?

I need to run a query with sorting, to get the rating of users, and accordingly show the user his place in the leader-bord.
I found how to execute the query, but I can't figure out how to call it from the code. I get either null or a query error.
$stmAt = $mysqli->multi_query("SET #rank=0;
SELECT RANK FROM
(
SELECT #rank:=#rank+1 AS 'RANK', user_id FROM
(
SELECT score, user_id
FROM `fishing_contest` GROUP BY user_id ORDER BY score DESC
) as t1
) as t2
WHERE user_id = ?";");
$stmAt->bind_param("i", $_SESSION['id']);
$stmAt->execute();
$data = $stmAt->get_result();
$userContest = $data->fetch_assoc();
I have a solution to use a normal query and by assigning a variable and a while loop to output the position, but it is too long. So I would like to know how to use and get a response from queries of this kind?

SELECT count rows from 2 tables and sum the results

I have the PHP code:
$query = mysqli_query($mysqli, "SELECT * FROM `table_1`");
$result = mysqli_num_rows($query);
$queryTwo = mysqli_query($mysqli, "SELECT * FROM `table_2`");
$resultTwo = mysqli_num_rows($queryTwo);
$number = $result + $resultTwo;
return $number;
The point is that sometimes, the $number variable is returning NULL,
when it should not supposed to do that.
I have always rows in those 2 tables, and the returned result should not be NULL, ever.
Is this a correct approach to sum the number of rows from 2 tables? I don`t understand why sometimes I get NULL instead of a number.
Well, I would suggest you to do it like
select ( select count(*) from Table1 ) + ( select count(*) from Table2 )
as total_rows
executing this query and getting the value of total_rows will return you true result
Or you can create a stored procedure to do the same thing. as explained below
CREATE PROCEDURE sp_Test
AS
-- Create two integer values
DECLARE #tableOneCount int, #tableTwoCount int
-- Get the number of rows from the first table
SELECT #tableOneCount = (SELECT COUNT(*) FROM Table1
WHERE WhereClause)
SELECT #tableTwoCount = (SELECT COUNT(*) FROM Table2
WHERE WhereClause)
-- Return the sum of the two table sizes
SELECT TotalCount = #tableOneCount + #tableTwoCount
Why don't you go with only one query like this:
you will have the result directly in one step and it will avoid contacting the DB twice to fetch intermediate result and it will also simplify your program!
SELECT
(select count(*) from table_1)
+
(select count(*) from table_2)

MYSQL SELECT including COUNT within the same table with reference to specific column

So, I'm getting data from one table with a simple SELECT statement with SELECT COUNT included.
SELECT *,
(SELECT COUNT(`message_id`)
FROM `user_message`
WHERE `status_to` = 0
) AS `unread`
FROM `user_message`
Here, unread counts for unread messages coming from other users. But that's not quite enough. What I want is to reference SELECT COUNT to a specific column, user_id, within the same table:
SELECT *,
(SELECT COUNT(`message_id`)
FROM `user_message`
WHERE `status_to` = 0 AND `user_message`.`user_id` = `user_message`.`user_id`
) AS `unread`
FROM `user_message`
.. if that makes sense. My second statement disregards this part: AND user_message.user_id = user_message.user_id, and gives me the same result for each user_id.
What am I missing??
You need to give different aliases to your table to get the related count
SELECT *, (
SELECT COUNT(`message_id`)
FROM `user_message` b
WHERE `status_to` = 0
AND `a`.`user_id` = `b`.`user_id`
) AS `unread`
FROM `user_message` a

Count entries from second Table that match id from first Table

This is basic but I can't figure it out. I have two tables (SeriesTable and OtherTable). SeriesTable has all the information for a given series including its id (column named "seriesid"). And OtherTable has a column called "seriescolumn" which also has the ids of a given series.
I need to make a query that counts every entry in OtherTable's "seriescolumn" that matches the seriesid column in SeriesTable. So for example, if the seriesid in SeriesTable is 5, I need to count how many entries in OtherTable have the value of 5 in the seriescolumn.
Below is my current code that simply grabs the info from the first table, but I have no idea how to correctly count the matching entries from OtherTable.
<?
$rs= mysql_query("SELECT seriesid FROM SeriesTable ORDER BY seriesid DESC");
while ($row= mysql_fetch_array($rs)) { ?>
content
<? } ?>
Sounds like you are going to need a join and group by statement.
SELECT s.seriesid, Count(*) As NumberOfSeries
FROM SeriesTable s Join
OtherTable o On s.seriesid = o.seriescolumn
Group By s.seriesid
ORDER BY seriesid DESC
This should return each seriesid and a count of how many times it was repeated.
Probably the easiest way to do this is in one big SQL query, using the count statement.
You can use a GROUP BY clause to group the result by the seriesid as you want, giving something along the lines of:
SELECT seriesid, COUNT(*) FROM SeriesTable, OtherTable
WHERE seriescolumn=seriesid GROUP BY seriesid
SELECT seriesid, COUNT(seriescolumn)
FROM SeriesTable, OtherTable
WHERE OtherTable.seriescolumn = SeriesTable.seriesid
GROUP BY seriesid;

I want to update a column if the row is duplicate

I have the following table named information.
As you see in the previous table there is column named number, this column has a many duplicate values.
What I want is, if the row is duplicate, update its tab cell to 1, otherwise, leave it as it is 0.
The following is what I done.
$query = mysql_query("UPDATE information SET tab='1' WHERE number = (SELECT distinct number FROM information)");
UPDATE information
SET tab = '1'
WHERE number IN (SELECT number
FROM (SELECT number
FROM information
GROUP BY number
HAVING count(*) > 1
) AS temp)
The subquery will return all duplicated number values.
Edit: I've tried it and MySQL shows 1093 error. I have just edited the query according to the solution found here.
I would do this with an update and join:
UPDATE information i join
(select number
from information i
group by number
having count(*) > 1
) idups
on i.number = idups.number
SET i.tab = '1';

Categories