Every derived table must have its own alias - mysql

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

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.

How to update multiple columns from different table of MariaDB

I am using MariaDB. I am trying to update two columns from SELECT different table.
UPDATE User U
SET
U.UserPoint = (
SELECT ((SELECT COUNT(*)
FROM CARD_COMM R
WHERE R.Card_ID = C.Card_ID) * 3
+
(SELECT COUNT(*)
FROM SECTION_CARD_LIKE L
WHERE L.Card_ID = C.Card_ID) * 1) as userPoint
FROM CARD C WHERE C.userid = U.userid ORDER BY userPoint DESC limit 1 )
this works
UPDATE User U
SET
(U.UserPoint, U.Card) = (
SELECT ((SELECT COUNT(*)
FROM CARD_COMM R
WHERE R.Card_ID = C.Card_ID) * 3
+
(SELECT COUNT(*)
FROM SECTION_CARD_LIKE L
WHERE L.Card_ID = C.Card_ID) * 1) as userPoint,
C.Card_ID as card
FROM CARD C WHERE C.userid = U.userid ORDER BY userPoint DESC limit 1 )
but this dose not....
How do I do this??
please help me...
Use a multi-table update, something like
UPDATE User
JOIN ( SELECT userid, up_value, card_value ... ) AS x
ON x.userid = User.userid
SET User.UserPoint = x.up_value,
User.Card = x.card_value;
(With suitable expressions/subqueries/etc for up_value & card_value)
You seem to be updating all rows in User??

Second query in a set not returning results

Can anyone see any reason why the second result set here produces 0 results even though if I run the queries independently the second one will return results??
SELECT GROUP_CONCAT(DISTINCT(mu.ID)) AS users
FROM `companies` c
JOIN `companysites` cs ON c.ID = cs.companyID
JOIN `users` mu ON cs.ID = mu.siteID
JOIN `targetcategories` tcat ON c.smTargetCategory = tcat.ID
WHERE isVendor = 0;
SET #in = "users";
SELECT *
FROM privileges
WHERE userID IN (#in);
I actually need the 2nd query to be a delete query but want to check results before I do it.
Pass your first query in a sub-query, for delete:
DELETE FROM privileges
WHERE userID IN (
SELECT GROUP_CONCAT(DISTINCT(mu.ID)) AS userID
FROM `companies` c
JOIN `companysites` cs ON c.ID = cs.companyID
JOIN `users` mu ON cs.ID = mu.siteID
JOIN `targetcategories` tcat ON c.smTargetCategory = tcat.ID
WHERE isVendor = 0
);
If mysql return error, you can try to 'encapsulate' your subquery:
DELETE p.*
FROM privileges p
WHERE userID IN (
SELECT userID
FROM
(SELECT GROUP_CONCAT(DISTINCT(mu.ID)) AS userID
FROM `companies` c
JOIN `companysites` cs ON c.ID = cs.companyID
JOIN `users` mu ON cs.ID = mu.siteID
JOIN `targetcategories` tcat ON c.smTargetCategory = tcat.ID
WHERE isVendor = 0) x);

How to check a record belongs to user in MySQL?

I've a SQL Query:
SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod
FROM `support_tickets_replies` r
LEFT JOIN `support_tickets` t ON (t.id = r.tid)
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid)
LEFT JOIN `users` responser ON (responser.id = r.uid)
WHERE r.tid = [something goes here]
I must check, does that ticket belongs to current user. User ID is in t.uid. When it's not that user, just returns column "error" with message "Forbidden". It's possible to do with only MySQL?
SELECT r.*, t.title, t.active, ticket_author.username as ticket_author, responser.username, responser.isAdmin, responser.isMod
FROM `support_tickets_replies` r
LEFT JOIN `support_tickets` t ON (t.id = r.tid)
LEFT JOIN `users` ticket_author ON (ticket_author.id = t.uid)
LEFT JOIN `users` responser ON (responser.id = r.uid)
WHERE r.tid = [something goes here]
AND t.uid = [User ID goes here]
This query will only turn up records that belong to the user.
If the record doesn't belong to the user, it will return nothing.

how to select previous record with LEFT JOINed tables in MySQL?

I'm struggleing to get the following to work:
# prev
SELECT klha.buyerID < PARAMETER_1
FROM agents AS v
LEFT JOIN seller_authorized AS klhs
ON klhs.agent= v.agentID
AND klhs.iln = v.seller
AND v.`status` = 'auth'
LEFT JOIN cust_list AS klha
ON klha.buyerID = klhs.userID
AND klha.accountNo = klhs.accountNo
WHERE v.iln = PARAMETER_2
AND klhs.acccountNo IS NOT NULL
ORDER BY klha.buyerID
LIMIT 1
;
END
The left joins are working as I'm using them in another procedure to select ALL relevent records. The problem is with my attempt to select the previous record.
I'm passing in a buyerID and need to get back the id of the previous record.
Question:
Can anyone point me to the correct syntax?
EDIT:
If I run this in MySQL, I'm getting "0" as resultset
Thanks!
SOLUTION:
Got it to work like this:
# prev
SELECT klha.buyerID
FROM agents AS v
LEFT JOIN seller_authorized AS klhs
ON klhs.agent= v.agentID
AND klhs.iln = v.seller
AND v.`status` = 'auth'
LEFT JOIN cust_list AS klha
ON klha.buyerID = klhs.userID
AND klha.accountNo = klhs.accountNo
WHERE v.iln = PARAMETER_2
AND klhs.acccountNo IS NOT NULL
AND klha.buyerID < PARAMETER_1
ORDER BY klha.buyerID
LIMIT 1
;
Easier than thought :-) Thanks #Henrique Ordine
If I understand correctly what you need, adding this condition to your where clause should do the trick:
and klha.buyerID = (select max(buyerID) from agents where buyerID < PARAMETER_1)
Like this:
SELECT klha.buyerID
FROM agents AS v
LEFT JOIN seller_authorized AS klhs
ON klhs.agent= v.agentID
AND klhs.iln = v.seller
AND v.`status` = 'auth'
LEFT JOIN cust_list AS klha
ON klha.buyerID = klhs.userID
AND klha.accountNo = klhs.accountNo
WHERE v.iln = PARAMETER_2
AND klhs.acccountNo IS NOT NULL
and klha.buyerID = (select max(buyerID) from agents
where buyerID < PARAMETER_1)
ORDER BY klha.buyerID
LIMIT 1