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;
Related
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??
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.
I currently have the following queries (and some surrounding vB code). I was hoping I could slim this up into a single SELECT statement versus having to run 10 more on each page to get that names of individuals.
$results = $db->query_read_slave("
SELECT user.id AS steam, user.skills AS level
FROM wcsp.wcgousers AS user
WHERE race = '_wcs_'
ORDER BY ABS(user.skills) DESC LIMIT 10
");
$rank = 1;
while ($user = $db->fetch_array($results)) {
$result = $db->query_first("
SELECT old.name AS name
FROM wcsp.warn_oldnames AS old
WHERE id = '$user[steam]'
ORDER BY lasttime
DESC LIMIT 1
");
$listing[] = array("id" => $user['steam'], "level" => $user['level'], "name" => $result['name'], "rank" => $rank);
$rank += 1;
}
I have tried LEFT JOIN but the issue I run into is that I would need a subquery in the LEFT JOIN similar to:
SELECT user.id AS steam, user.skills AS level, names.name AS name
FROM wcsp.wcgousers AS users
LEFT JOIN
(
SELECT names.name
FROM wcsp.warn_oldnames AS names
WHERE id = wcsp.wcgousers.id
ORDER BY lasttime DESC LIMIT 1
) AS names
ON names.id = users.id
WHERE users.race = '_wcs_'
Which won't work due to the different database check inside the subquery.
If I understand you correctly, you want to get the latest Name for every users.
SELECT a.id AS steam,
a.skills AS level,
b.name
FROM wcgousers a
INNER JOIN warn_oldnames b
ON a.ID = b.ID
INNER JOIN
(
SELECT ID, MAX(lasttime) max_date
FROM warn_oldnames
GROUP BY ID
) c ON b.ID = c.ID AND
b.lastTime = c.max_date
WHERE a.race = '_wcs_'
-- ORDER BY ABS(a.skills) DESC
-- LIMIT 10
I have this MySQL Query (working)
First query:
SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (
SELECT *
FROM user_counter
WHERE users.id = user_counter.publisher_id
)
The MySQL query above is to find the user id from two table
Now I want to compared again using this second MySQL query (working)
Second query:
SELECT users.id, publisher_amount, publisher_now, publisher_max, counter
FROM users
INNER JOIN user_counter ON users.id = user_counter.publisher_id
WHERE no = 08123456789
AND counter < publisher_amount
But when I join all the query like this:
SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (
SELECT *
FROM user_counter
WHERE users.id = user_counter.publisher_id
)
AND (
SELECT users.id, publisher_amount, publisher_now, publisher_max, counter
FROM users
INNER JOIN user_counter ON users.id = user_counter.publisher_id
WHERE no =08123456789
AND counter < publisher_amount
)
I get this error:
Operand should contain 1 column(s)
Then, I try using 1 column, but the result is not what I wanted.
My question is How to join first and second query ? and produce no error.
I have tried google it and after many "try-and-error" this is the far I can get to make the query work.
I think you just miss an EXISTS on your second subquery. Anyway, if I understand your query correctly, I think you could write your query as this:
SELECT
u.id
FROM
users u inner join user_counter uc
on u.id=uc.publisher_id
and no=08123456789
and counter < publisher_amount
WHERE
u.publisher_set = '1'
AND u.publisher_status = '1'
AND u.publisher_content != ''
AND u.publisher_amount != '0'
AND u.publisher_now < publisher_max
You could change the first EXISTS clause to:
and users.id in (select user_counter.publisher_id from user_counter)
and use EXISTS on the final query.
You could also do this:
AND EXISTS (
SELECT user_counter.publisher_id
FROM user_counter
WHERE users.id = user_counter.publisher_id
PS: SQLFIDDLE doesn't work in my end for some reason. Else would have been happy to give you a demonstration ;)
SELECT id
FROM users
WHERE publisher_set = '1'
AND publisher_status = '1'
AND publisher_content != ''
AND publisher_amount != '0'
AND publisher_now < publisher_max
AND EXISTS (
select 1 from user_counter where users.id = user_counter.publisher_id
and no =08123456789
AND counter < publisher_amount
)
Assuming no and counter are on table user_counter. A bit hard to tell without a schema.
I have query like this:
SELECT `all_messages`.`user_1`, `messages`.*, `users`.`username`
FROM `all_messages`
JOIN `messages` ON (`all_messages`.`user_2` = `messages`.`from_user`)
JOIN `users` ON (`all_messages`.`user_2` = `users`.`id`)
WHERE `all_messages`.`user_1` = '12'
ORDER BY `messages`.`sent` DESC LIMIT 2
Now this query does what I need but my problem is with this line
ON (`all_messages`.`user_2` = `messages`.`from_user`)
It selects all data from messages where the matches was found but I need only one newest record. I hope you guys get what I mean.
If you need one "newest record" you should have a date column or something, lets name it "CREATION_TIME", so you could do something like this
SELECT AM.user_1, M.*, U.username
FROM all_messages AM, messages M , users U
WHERE AM.user_1 = '12'
AND AM.user_2 = M.from_user
AND AM.user_2 = U.id
AND M.CREATION_TIME =
(
SELECT MAX(CREATION_TIME)
FROM messages
WHERE from_user= M.from_user
)
ORDER BY M.sent DESC LIMIT 2
Edit
SELECT AM.user_1, M.*, U.username
FROM all_messages AM, messages M, users U
WHERE AM.user_1 = '12'
AND AM.user_2 = M.from_user
AND AM.user_2 = U.id
AND M.sent =
(
SELECT MAX(sent)
FROM messages
WHERE from_user= M.from_user
)
ORDER BY M.sent DESC LIMIT 2
It should work