WHERE id IN doesn't work in UPDATE - php

UPDATE statistics'
SET money = money + '$money'
WHERE member_id IN ((SELECT member_id FROM races WHERE l_id = '$mem_id'), $other_id)
What's wrong with that? I want to retrieve all member_ids from races and also include to member_id $other_id. Without $other_id it works.
By the way, it gives me "Subquery returns more than 1 row" error.

Try with:
UPDATE statistics
SET money = money + $money
WHERE member_id IN (
SELECT member_id
FROM races
WHERE l_id = $mem_id
)
OR member_id = $other_id
And suggestion - for int type columns do not use apostrophs.

subquery returns member_id and $other_id

Another way to do it:
(SELECT member_id FROM races WHERE l_id = '$mem_id'
UNION
SELECT $other_id)

Related

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)

how to insert data in max id by using sql query

I want to get the maximum id of row data. In my table first column is id then firstname. this is the sql command I used to get the max(id) of row data and insert firstname in max(id) row. but it is not working
<?PHP $maxid=mysql_query("insert into test(firstname) values ('$sym') where id =max('id') ;?>
You can achieve your requirement in this way.
UPDATE test
SET firstname = '$sym'
ORDER BY `id` DESC LIMIT 1;
UPDATE yourtable
SET firstname = '$sym'
WHERE id in (SELECT max(id) from yourtable)

PHP mysqli Select Add field

I have a table that looks like this:
id | user_id | credits
----------------------
1 | 2 | 300
2 | 2 | 200
3 | 4 | 100
I need a select or way to add the credits from a specific user id
The select would be:
SELECT credit FROM myTable WHERE user_id ='2'
The result I would need in this case would be 500
How can I do this?
Use SUM() in your query will solve the issue.
SELECT SUM(`credits`) AS `total` FROM `myTable` WHERE `user_id` =2
Since you specify PHP tag, the below code will help you.
$mysqli=mysqli_connect("hostname","username","password","databasename");
$query = "SELECT SUM(`credits`) AS `total` FROM `myTable` WHERE `user_id` =2";
$processquery = $mysqli->query($query);
$result = $processquery->fetch_assoc();
echo $result['total'];
Use sum()
SELECT sum(credit) as sum_credit
FROM myTable
WHERE user_id = 2
Use aggregate function SUM():
SELECT SUM(credit) as TotalCredit
FROM myTable
WHERE user_id='2'
Read more about aggregate functions here.
Try SUM() in mysql
SELECT SUM(credit) as totalcredit FROM myTable WHERE user_id ='2'
will return you matched result sum according to condition
select sum(credit) as total from myTable where user_id = '2'
You can do this by summing the total of the credit column. Check out the MySQL reference manual for the official instructions.
SELECT SUM(credit) AS credit_total
FROM myTable
WHERE user_id = 2
You use the AS keyword to assign a name to your column, then in PHP you can access this column as you would any other by name, just using credit_total. E.g:
$query = "SELECT SUM(credit) AS credit_total
FROM myTable
WHERE user_id = 2";
$result = $mysqli->query($query);
$row = $result->fetch_assoc();
echo $row['credit_total']; // Will output 500

Mysql stored procedure with foreach

I am new to stored procedure
select distinct(movie_id) from movielist where mdate = ?
This return movie_id(s) and I need to run 3 sql for each movie_id
foreach(movie_id){
select count(*) as total_viewed from movielist where mdate = ? and movie_id = ? limit 1 //return only one value
select count(*) as total_viewed from movielist where mdate like ? and movie_id = ? limit 1 //return only one value
select count(*) as total_viewed from movielist where mdate like ? and movie_id = ? limit 1 //return only one value
//Then all the 3 value with movie_id and mdate insert at another table
}
Now how to write the stored procedures for this...
I guess you want to show the number of views based on each movie
select count(*) as total_viewed from movielist group by movie_id
this will show you all the counts based on movie_id
select count(*) as total_viewed from movielist group by mdate,movie_id
will show you all counts based on date and movie..
You can use UNION ALL to merge the results (just a hint, can be used, don't know what do you actually want).
You could use a cursor, but you do not need at all.
I am not sure what your stored procedure should do, but looks like to give the top 3 viewed movies on a particular day? If so just do
select movie_id, count(*) as total_viewed from movielist
where mdate = ?
group by movie_id
order by total_viewed desc
limit 0,3;

How to write mysql5 query for finding duplicate rows from a table?

Hi I have a table like this
ID UserName
1 test#test.com
2 test#test.com
3 john#stack.com
4 test#test.com
5 adam#stack.com
6 john#stack.com
I need an output like this. I need only repeated rows list. How can I create this kind of an output using mysql query.
ID UserName Count
1 test#test.com 3
2 john#stack.com 2
Please help me.
Thanks.
I had the same problem some time ago and solved it like this (as far as I remember):
SELECT *
FROM tableA INNER JOIN
(SELECT DISTINCT MAX(id) as id, type_id, temp FROM tableA GROUP BY type_id, temp) AS t
ON tableA.id = t.id
AND tableA.type_id = t.type_id
AND tableA.temp = t.temp
You join the table with itself selecting the ids that are duplicate. The fields that should be tested against duplicate values are in this case type_id and temp. If you need more or less fields that should be considered as duplicates you can adjust the fields.
I don't know if this helps in your case and if it can be done in a more simple way, so I'm prepared for downvotes ;-)
Edit: removed last condition AND tableA.id < t.id as suggested by ypercube because it leads to 0 results.
It looks like you're trying to pull the following data:
First ID for a given UserName
The UserName itself
The total number of IDs for that UserName
This query should do the trick:
SELECT
MIN(id),
UserName,
COUNT(id)
FROM users
GROUP BY UserName;
since the ID is not unique so its a bit not logical to get the sum of unique UserName from the table.
If the ID is not required we can get the result from single query.
SELECT UserName, COUNT(UserName) AS Count
FROM TableName GROUP BY UserName
HAVING COUNT(UserName) > 1;
But in the case of ID in the result it will be a more complicated query including sub-query and inner table.
SELECT UserName
, COUNT(*) AS `Count`
FROM tableX
GROUP BY UserName
HAVING COUNT(*) > 1
Hi this is the right answer.
SELECT UserName, COUNT(UserName) AS Count
FROM TableName GROUP BY UserName
HAVING COUNT(UserName) > 1;

Categories