How to use JOIN in UPDATE mysql query - mysql

Hello I want to bind this to one query with JOIN.
How does this work:
Db::bind("uid", strip_tags($userid));
DB::bind("user_id", strip_tags($refer));
Db::bind("points_earn", strip_tags($points_earn));
Db::bind("points_refer", strip_tags($points_refer));
Db::query("UPDATE referrals SET `points_earn` = :points_earn WHERE new_id = :uid");
Db::query("UPDATE users SET `points` = `points` + :points_refer WHERE id = :user_id");
What I think but not work.
Db::query("UPDATE referrals r JOIN users u ON r.user_id = u.id SET `r.points_earn` = :points_earn WHERE r.new_id = :uid AND `u.points` = `u.points` + :points_refer WHERE u.id = :user_id");
Have anyone a solution?

Your syntax is right, but the back ticks are wrong and each SELECT only has one WHERE:
UPDATE referrals r JOIN
users u
ON r.user_id = u.id
SET r.points_earn = :points_earn
WHERE r.new_id = :uid AND u.points = u.points + :points_refer AND
u.id = :user_id;
The problem with backticks is that the following are quite different:
`r.points_earn`
`r`.`points_earn`
The first refers to a column called "r.points_earn". The second refers to the "points_earn" column in "r". The backticks aren't necessary so you can just remove them.

Related

How to fix join error in mysql update query

I had an error in mysql as picture. please help me
sql code is following:
UPDATE
tbl_users AS Users
SET
Users.money_current = Users.money_current +
CASE
WHEN TempTbl.money_info IS NULL
THEN 0
ELSE TempTbl.money_info
END
LEFT JOIN
(SELECT
userId,
SUM(bet_money * bet_rate) AS money_info
FROM
tbl_betting
WHERE ROUND = 'xxx'
AND is_win = 1
GROUP BY userId) AS TempTbl
ON Users.userId = TempTbl.userId
FROM tbl_users AS Users;
This is the correct syntax:
UPDATE tbl_users AS Users
LEFT JOIN (
SELECT userId, SUM(bet_money * bet_rate) AS money_info
FROM tbl_betting WHERE ROUND = '965802' AND is_win = 1
GROUP BY userId
) AS TempTbl ON Users.userId = TempTbl.userId
SET Users.money_current = Users.money_current + COALESCE(TempTbl.money_info, 0)
I also changed that CASE expression with COALESCE().
But I think an INNER JOIN would also work in your case, since the unmatched rows of the LEFT JOIN that you use do not change the value of money_current.

Get other different id

I have a simple mysql query:
SELECT U.id FROM
users U
INNER JOIN friends F
ON ( U.id = F.id_exp AND F.id_des = :id_exp )
OR ( U.id = F.id_des AND F.id_exp = :id_exp ) WHERE
U.id <> :id_des
AND F.active = 1
I wish that by calling this request again, that It does not return the id that It has already previously returned.
I hope you understand me.
Thank you.
You must give RAND a seed number in order to always get the same order. This can be any number, e.g. 1:
...
ORDER BY RAND(1)
LIMIT #offset, 1;

using mysql what will be the fastest way to join tables

i have two tables "User" And "UsersSettings" .
and i want to select User's where the users settings is something
what query will be more fast ?
SELECT u.*, us.someSettings FROM User u,UserSettings us
WHERE u.id = us.user_id
AND us.somesettings = somevalue
AND u.someProperty = someOtherValue
AND u.someProperty1 = someOtherValue1
AND u.someProperty2 = someOtherValue2
AND u.someProperty3 = someOtherValue3
AND u.someProperty4 = someOtherValue4
AND us.someUSProperty = someUSvalue
OR
SELECT u.*, us.someSettings FROM User u
LEFT JOIN UserSettings us ON us.user_id = u.id
WHERE us.somesettings = somevalue
AND u.someProperty = someOtherValue
AND u.someProperty1 = someOtherValue1
AND u.someProperty2 = someOtherValue2
AND u.someProperty3 = someOtherValue3
AND u.someProperty4 = someOtherValue4
AND us.someUSProperty = someUSvalue
can you help me please
thank you
The correct query is an inner join:
SELECT u.*, us.someSettings
FROM User u INNER JOIN
UserSettings us
ON us.user_id = u.id
WHERE us.somesettings = somevalue
AND u.someProperty = someOtherValue
AND u.someProperty1 = someOtherValue1
AND u.someProperty2 = someOtherValue2
AND u.someProperty3 = someOtherValue3
AND u.someProperty4 = someOtherValue4
AND us.someUSProperty = someUSvalue;
You have a filtering condition on UserSettings, so the outer join is turned to an inner join anyway. You should avoid implicit join syntax and put the join conditions explicitly in an on clause. This is for readability and maintainability. Both versions should have the same performance.

MySQL Unknown column u.id in on clause but column exists

I've got this tables:
users {id = int, name = varchar, pwd = char}
company {id = int, token = char, name = varchar}
user_company {id = int, id_usr = int, id_company = int, name_usr = varchar}
I'm trying to get the pwd from users and find out if the user is in the company with the token X from user_company
When I use this query
SELECT u.pwd,h.name_usr
FROM users u, company c
LEFT JOIN users_company h ON c.id = h.id_company AND u.id = h.id_usr
WHERE u.user_name = 'user#domain.com'
AND c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LIMIT 1;
I keep on getting 'Unknown column u.id in on clause' although u.id exists. What am I doing wrong? Thanks
SELECT u.pwd,h.nombre_usr
FROM company c LEFT JOIN users_company h ON c.id = h.id_company
LeFt join users u on u.id = h.id_usr
WHERE u.user_name = 'user#domain.com'
AND c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LIMIT 1;
The Error was beacause of Join Query . The way it was written is wrong. We can not apply join two tables with single table at same time.
sI've got the solution. Thank you all for the help.
SELECT u.pwd,h.name_usr
FROM users u
LEFT JOIN company c ON c.token = 'f30ea71e7a9d9f0a6710bb46537c0bde'
LEFT JOIN users_company h ON c.id = h.id_company AND u.id = h.id_usr
WHERE u.user_name = 'user#domain.com'
LIMIT 1;

Every derived table must have its own alias

I have the following query:
SELECT `snap`.`ID`, `user`.`username`, `vote`.`type`
FROM (`snap`) JOIN `user` as u ON `u`.`ID` = `snap`.`user`
LEFT JOIN (select * from vote where user = "18") as vote ON `snap`.`ID` = `vote`.`snap`
JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM snap)) AS x ON `snap`.`ID` => `x`.`ID`
WHERE `snap`.`active` = 0 LIMIT 1
It worked just fine untill I added the last JOIN. Now i get the error: "Every derived table must have its own alias". I know it is because every table needs its on alias and I need to put "as S" or something somewhere, but I can't find out how to do that in this query.
Seems like you have extra closing parenthesis after snap. It should be 1 instead of 2 closing parentheses.
SELECT `snap`.`ID`, `user`.`username`, `vote`.`type`
FROM (`snap`) JOIN `user` as u ON `u`.`ID` = `snap`.`user`
LEFT JOIN (select * from vote where user = "18") as vote ON `snap`.`ID` = `vote`.`snap`
JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM snap) AS x ON `snap`.`ID` = `x`.`ID`
WHERE `snap`.`active` = 0 LIMIT 1
Correct syntax is :
SELECT `snap`.`ID`, `user`.`username`, `vote`.`type`
FROM (`snap`) JOIN `user` as u ON `u`.`ID` = `snap`.`user`
LEFT JOIN (select * from vote where user = "18") as vote ON `snap`.`ID` = `vote`.`snap`
JOIN (SELECT CEIL(MAX(ID)*RAND()) AS ID FROM snap) AS x ON `snap`.`ID` = `x`.`ID`
WHERE `snap`.`active` = 0 LIMIT 1