How to update multiple fields - php

I have a table called users .
Users {name, team_id, overall_user_score}
I want to select all from a team (eg all from team 2) and add 1 to each users overall score taking into account the current score of each user.
I have the first part of the MySQL done, But I cant figure out how to actually update the score.
SELECT * from users WHERE team_id = $team UPDATE overall_user_score

Here is the correct SQL Query for updating.
UPDATE `users` SET `overall_user_score`=`overall_user_score`+1 WHERE team_id= "'.$team.'"

UPDATE `users` SET `overall_user_score`=overall_user_score + 1 WHERE team_id= "'.$team.'"
this should work

Related

Select User id from one table and update in another table

I have two table called tbl_users and tbl_img_status. I have column in tbl_img_status like below
id, user_id,status_text, scd_time,status,post_time
I am looking for run cron using PHP for publish post on time. So I have query like below
$results = mysqli_query($mysqli,"UPDATE tbl_img_status SET post_time=NOW(),status=1 WHERE status=0 AND scd_time<NOW()");
Now My issue is I also need to update tbl_users column called total_post. I want increase 1 in total_post of that user id which we have published with first query. I am new in PHP and MYSQL so not getting proper idea for do it. Let me know if someone can help me for it.
You can use one query to update two tables.
Try this query if that works.
I have got a hint from this. MySQL, update multiple tables with one query
UPDATE tbl_users, tbl_img_status
SET tbl_img_status.post_time=NOW(),
tbl_img_status.status=1,
tbl_users.total_post = tbl_users.total_post+1
WHERE
tbl_users.id= tbl_img_status.user_id
AND tbl_img_status.status=0 AND tbl_img_status.scd_time<NOW()
You can use Triggers for that purpose.
This would update for a specific user if the status changed from 0 to 1
DELIMITER $$
CREATE TRIGGER after_tbl_img_status_update
AFTER UPDATE
ON tbl_img_status FOR EACH ROW
BEGIN
IF OLD.status <> NEW.status AND NEW.status = 1 THEN
UPDATE tbl_users SET total_post = total_post +1 WHERE id = NEW.user_id;
END IF;
END$$
DELIMITER ;
IF you don't want to change the Database, you can use a INNER JOIN with And update both table as one.
Your php will then look like:
$results = mysqli_query($mysqli,"UPDATE tbl_users tu INNER JOIN tbl_img_status tis ON tu.id = tis.user_id SET tis.post_time=NOW(), tis.status=1, tu.total_post = tu.total_post+1 WHERE tis.status=0 AND tis.scd_time<NOW();");

Mysql trigger from one table to another

So, I have a table A that each time a user sends an image, a record is created storing the time it was uploaded, the username of the user and the image number out of all the images uploaded over time.
I need to make a second table B that will store the amount of images uploaded per user and the user name. I need this table B to be updated when a new entry is generated in A.
I found that a trigger function can be created, nevertheless I'm having a rough time finding an example that will suit my needs.
Does anyone know a way of doin what I want?
Just update b table with a select count of total inserted records on a from current user NEW.userid (userid is your column name or whatever name you have there, and NEW is a fixed mySql reference for the current values to be inserted):
CREATE TRIGGER img_sum AFTER INSERT ON a
FOR EACH ROW SET b.total = (SELECT COUNT(*) FROM a WHERE a.userid=NEW.userid)
WHERE b.userid = NEW.userid;
From what you have described i don't think you need a second table. You can just count the number of time a user name has occurred, and you will get the number of images that user has uploaded.
You can get the count doing something like that
SELECT COUNT(DISTINCT username) FROM table_name;
If you still need to create 2 tables, you might want to take a look at procedures and how they work.
Let's say we have 3 tables for this case:
- users(id, username, email ....),
- user_images(id, userId, image_num, date_uploaded)
- user_images_count(id, user_name, images_count)
The user_images_count is initially empty. We have to fill it up by such query:
INSERT into user_images_count(user_name, images_count)
SELECT (select username from users where ui.userId = id) as username, count(userId) as counter FROM `user_images` ui group by ui.userId;
Then, we must immediately create the trigger that will process every INSERT operation into user_images table.
CREATE TRIGGER `count_user_images` AFTER INSERT ON `user_images`
FOR EACH ROW begin
declare u_name tinytext default "";
set u_name = (select username from users where id = NEW.userId limit 1);
if(u_name != "") then
update user_images_count set images_count = images_count + 1 where user_name = u_name;
end if;
end
This two queries (user_images_count fulfillment and trigger creation must be performed in one transaction, one by one).
I've created similar triggers on my local databases. They work pretty good. )))

Database update needs FROM clause, but FROM clause causes error

I've been trying to figure out how to update the ID of the newest person in my database for 36 hours. It moans about the clients in the FROM clause, but when I remove that clause, the update affects every ID in the whole database.
UPDATE clients SET ID = $id WHERE timestamp = (SELECT MAX(timestamp) FROM clients)
What am I doing wrong?
Replace it with
UPDATE clients SET ID = $id ORDER BY `timestamp` DESC LIMIT 1
PS: this query solves the original task specified in the question "to update the id of the newest person in my database"
You can't update a table using a WHERE condition aggregated from the exact same table.
perhaps you want this:
UPDATE
client
SET
client.[id] = $id
ORDER BY
client.[timestamp] DESC
LIMIT 1

query to update record with another random record from another table?

im having a problem with a small issue within this query
$update = mysql_query("UPDATE earnings SET userid = (SELECT ID FROM users WHERE installid is NOT NULL ORDER BY rand() LIMIT 1) WHERE userid='0'");
this query update the userid within the earning table when it value of '0'
what i need to do is to update the userid where its not found within the user table
for example
Earnings table has 5 entries where userid=10
the userid 10 is not found with in the users table and users table have those ids (1'2'3'4'5)
then update this userid which have the value 10 with any of the ids found within the users table and have the installid not nulled
I'd imagine a customer would want their earnings data, of all things, to be relevant, not completely random (if some if the data is random and you don't know which, all of the data is corrupted). That said, the query gets you where you want with
UPDATE earnings SET userid = (SELECT ID FROM users WHERE installid is NOT NULL ORDER BY rand() LIMIT 1) WHERE userid NOT IN (SELECT ID FROM users)
What I'd do instead is put a foreign key on the ID and prevent the random data being written in in the first place. I cannot see a solution where having random IDs would be better than not having those rows at all. If the IDs are not important, why have them at all?

How to query one table and add rows to another using that first query? MySQL

I have some users setup in a MySQL table with different variables. I am trying to figure out what would be the best way to do this. Basically I want to award all of my registered and active users with bids which are stored in another table.
So for the Table "users" I have ran this query:
SELECT *
FROM `users`
WHERE `active` = 1
AND `admin` = 0
ORDER BY `users`.`id` ASC
Which will show all active users who are not administrators.
Now I would like to give each one of these users which are identified by the "ID" field in another table.
So in the "bids" table I would need to add a new row for each one of those users with all of the same values except for the "user_id" field which will basically match the "id" field of the table "users"
What would be the best approach for this. There are approximately 6,000+ users coming up in the first query.
Can you do something like this?
INSERT INTO bids
(col1, col12, col3)
SELECT
users.col1, users.col2, users.col3 FROM users
WHERE users.active = 1 and users.admin = 0
ORDER BY users.id ASC
Typically not a good idea to duplicate data like that but there is also:
create table bids as select <fields> from users

Categories