TABLE1
mybb_users
(id gcoins)
1 150
2 10
TABLE2
servercoins
(id prezzo)
1 150
2 70
I should compare this table for control if users have enough "gcoins" for buy "prezzo". If "gcoins" > "prezzo" it's ok but "gcoins" < "prezzo" print an alert with "You don't have enough gocins for this"
Try this...
SELECT
CASE WHEN ((SELECT gcoins
FROM TABLA1
WHERE TABLA1.id = <ID_USER>
AND gcoins >0) < ( SELECT prezzo
FROM TABLA2
WHERE TABLA2.id = <ID_PREZZO>))
THEN 'You dont have enough gocins for this'
ELSE 'something else...'
END
<ID_USER> and <ID_PREZZO> replace with your variables.
HERE you can test!
Hope this help!
This is a general query that will print out the status:
SELECT u.id, u.gcoins, s.prezzo,
CASE WHEN u.gcoins >= s.prezzo THEN 'OK' else 'Not Enough gcoins' END AS Status
FROM mybb_users u
INNER JOIN servercoins s ON u.id = s.id
If you want a query that just returns the rows that need the alert, then remove the second line, and add a WHERE clause instead:
SELECT u.id, u.gcoins, s.prezzo
FROM mybb_users u
INNER JOIN servercoins s ON u.id = s.id
WHERE u.gcoins < s.prezzo
This gives you a list of those who need to be warned that they don't have enough gcoins.
Related
review table has store_idx, user_idx etc...
I want to create a query sentence that gets information about the store to which the user has bookmarked with the user_id value entered.
The query sentence I made is
select A.store_name
, A.store_img
, count(B.store_idx) as review_cnt
from board.store A
Left
Join board.review B
On A.store_idx is B.store_idx
where store_idx is (select A.store_idx from bookmark where user_id = ?)
However, nothing came out as a result.
Help me..
Please use below Query:
SELECT store_name
, store_img
, SUM(review_cnt) AS review_cnt
FROM
( SELECT DISTINCT A.store_name
, A.store_img
, CASE WHEN B.store_idx IS NULL THEN 0 ELSE 1 END AS review_cnt
FROM bookmark br
JOIN board.store A
ON A.store_idx = br.store_idx
LEFT
JOIN board.review B
ON A.store_idx = B.store_idx
WHERE br.user_id = ?
)T
The WHERE clause is obviously filtering out all rows. We can't do much about that. But your query is also lacking a GROUP BY, the table aliases can be improved, and the join condition is not correct.
So, try this version:
select s.store_name, s.store_img, count(b.store_idx) as review_cnt
from board.store s left join
board.review r
on s.store_idx = r.store_idx
where b.store_idx in (select b.store_idx
from bookmark b
where b.user_id = ?
);
I have a problem in my SQL Select , for example we have two users X and Y , user X want to see user Y Following so , when see his following ,
user Y want to know who users in user X (following) follow them
in this picture : https://i.imgsafe.org/d2b2d83886.png user with id :68 want to see users.id = 50 Following
SELECT users.id,users.username,
CASE WHEN follows.user_id = 68 THEN 1 END AS is_Follow
FROM users
LEFT JOIN follows
ON follows.follower_id = users.id
WHERE follows.app = 1 AND follows.user_id = 50
but this return me NULL :( what should i do ?!
Conditions on the right table of a LEFT JOIN should be on the ON clause and not on the WHERE clause :
SELECT users.id,users.username,
CASE WHEN follows.user_id = 68 THEN 1 ELSE -1 END AS is_Follow --Minus one value means NO.
FROM users
LEFT JOIN follows
ON follows.follower_id = users.id AND
follows.app = 1 AND
Another thing, what exactly are you checking? How many people follow user_id=68 ? Because by the name of the column it doesn't sounds like it.
EDIT: Try this:
SELECT users.id,users.username,
MAX(CASE WHEN follows.user_id = 68 THEN 1 ELSE -1 END) AS is_Follow --Minus one value means NO.
FROM users
LEFT JOIN follows
ON follows.follower_id = users.id AND
follows.app = 1 AND
GROUP BY users.id,users.username
I have a problem regarding mysql.
I have few tables like tbl_follow, tbl_user, tbl_post_like,
Now what I want is that my query should return the result like:
i want userid, username, user image, user state from one table->tbl_user,
then i want those users who are following other friend(other user) only if the column "estatus" in table "tbl_follow" contain value as "Active" otherwise if there is an "inactive" value then it should return 0 as i have coded in query.
tbl_follow : 1.iFollowID, 2.iUserID, 3. iFriendID, 4. eStatus
tbl_post_like: 1. iPostLikeID, 2. iUserID, 3. iPostID
tbl_user: 1.iUserID, 2. vUsername, 3. vImage, 4. vState
Now the simple query I am using is:
SELECT u.iUserID,
u.vUsername,
u.vState,
u.vImage,
(SELECT IF(
(SELECT iFollowID
FROM tbl_follow
WHERE iUserID =249
AND iFriendID = 250
AND eStatus = 'Active')!='', 1,0)
FROM tbl_follow) AS is_follow
FROM tbl_user u
INNER JOIN tbl_post_like l ON u.iUserID=l.iUserID
WHERE l.iPostID=21
Not sure about performance on this in comparison to using an additional join, but hopefully it'll work:
Try:
SELECT u.iUserID,
u.vUsername,
u.vState,
u.vImage,
IF(EXISTS
(SELECT iFollowID
FROM tbl_follow
WHERE iUserID =249
AND iFriendID = 250
AND eStatus = 'Active'), 1,0) AS is_follow
FROM tbl_user u
INNER JOIN tbl_post_like l ON u.iUserID=l.iUserID
WHERE l.iPostID=21
I'm trying to write a query that returns true if the same idx exists in another table.
Here is what I want to do.
Two Tables:
User (user_idx, name)
Group (group_idx, user_idx)
Pseudo Query:
SELECT user_idx, name, (True(1)/False(0) value) as has_joined_group
FROM User
WHERE (Check if user_idx exists in Group table where group_idx is 3)
Can this be done using Mysql? If yes, how?
SELECT u.user_idx, u.name, g.user_idx IS NOT NULL AS has_joined_group
FROM User AS u
LEFT JOIN Group AS g
ON g.user_idx = u.user_idx AND g.group_idx = 3
SELECT
u.user_idx,
u.name,
CASE WHEN g.user_idx IS NOT NULL AND g.group_idx = 3 THEN 1 ELSE 0 END AS has_joined_group
FROM
user u JOIN LEFT group g ON u.user_idx = g.user_idx
SELECT u.user_idx, u.name, if(g.user_idx IS NOT NULL,'true','false') as has_joined_group
FROM User AS u
LEFT JOIN Group AS g
ON g.user_idx = u.user_idx AND g.group_idx = 3
i have a details table with columns:
user_id int
code int
value int
And i want to build a summary table that looks like:
user_id int
valueA int
valueB int
In the details table, valueA would correspond to say, code 5, and valueB would correspond to say, code 6, so i'm looking for something like:
insert into summary (user_id,valueA,valueB) VALUES ( SELECT ??? from details );
The problem of course is that i'm looking at multiple rows from the "details" table to populate one row in the "summary" table.
Eg, if i had the following rows in details:
1 5 100
1 6 200
2 5 1000
2 6 2000
I want to end up with the following in the summary table:
1 100 200
2 1000 2000
Any ideas?
MySQL doesn't have PIVOT/UNPIVOT syntax, which leaves you to use a combination of GROUP BY and CASE expressions:
INSERT INTO SUMMARY
(user_id,valueA,valueB)
SELECT d.user_id,
MAX(CASE WHEN d.code = 5 THEN d.value ELSE NULL END),
MAX(CASE WHEN d.code = 6 THEN d.value ELSE NULL END),
FROM DETAILS d
GROUP BY d.user_id
insert into summary (user_id,valueA,valueB)
SELECT a.user_id, a.value, b.value
from details a
join details b on a.user_id = b.user_id
WHERE a.code = 5 and b.code = 6;
beware: you will end up with multiple summary columns if user_id+code is not unique.
EDIT:
insert into summary (user_id,valueA,valueB)
select u.user_id, ifnull(a.value,0), ifnull(b.value,0)
from (select distinct user_id from details /* where code in (5,6) */) u
left join details a on a.user_id = u.user_id and a.code = 5
left join details b on b.user_id = u.user_id and b.code = 6
If you have a manageable set of codes (say just 5 and 6) you could do something like this:
SELECT details.user_id, code5.value, code6.value
FROM details JOIN
(SELECT user_id, value FROM details WHERE code = 5) AS code5 USING(user_id)
JOIN
(SELECT user_id, value FROM details WHERE code = 6) AS code6 USING(user_id);
You may need to modify your JOINs depending on if your codes are not required as 1 to 1 relationship (i.e. LEFT JOINs).
If you have a large set of codes, I would look into a cursor runs a similar query above over a result set of your codes or using a different technology, (i.e. PHP script).