I'm trying to assign an ID code by searching for the name.
update db.request r
set s.CreateByID = (
select r.ID
from db.user u
where r.NameCreateBy = u.Name
)
where r.ID in (23506);
How would I put a case clause for when I can't find the name in the user table?
The request table has the correct name of the person who created the request. But the createByID column is returning a code referring to another user. So I need to update createByID based on the user ID, disregarding the value shown in the request table.
you can use COALESCE if the subquery returns NULL amd set a value for example 0
update db.request r
set r.CreateByID = COALESCE((
select u.ID
from db.user u
where r.NameCreateBy = u.Name
),0)
where r.ID in (23506);
Consider using a JOIN inside the UPDATE statement, if you need to match fields between two tables:
UPDATE db.request r
INNER JOIN db.user u ON r.NameCreateBy = u.Name AND r.ID IN (23506)
SET s.CreateByID = COALESCE(r.ID, <substitution_value>);
Related
I have a database with two tables, (URL and VOTE).
I need to count all the votes in VOTE and include the result in the URL table
I have tried to do it as follows but it gives me error ** You can't specify target table 'url' for update in FROM clause**
UPDATE url
set up = (select (SELECT COUNT(*) FROM vote v WHERE v.id = u.id AND v.note = 1 ) as num from url u)
How could you do this?
I think you just want a simple subquery:
UPDATE url u
SET up = (SELECT COUNT(*)
FROM vote v
WHERE v.id = u.id AND v.note = 1
) ;
I'm not sure why you would want to be referring to url in the subquery.
I'm using MySQL 5.5.37. I want to update a table and I would like to use the value of the specific row I wish to update the in the query clause I use to get an update value. So for instance, I alias my table like so
update session ts set
and so I would like to reference "ts.id" in the "set" portion of the statement, if that's possible. I tried
update session ts set user_id = (select q.* from
(select u.id FROM session t, training_session_org tso, organization o, user u
where o.user_id = u.id and tso.organization_id = o.id
and t.id = tso.training_session_id and t.id = ts.id) q);
But I'm getting a
ERROR 1054 (42S22): Unknown column 'ts.id' in 'where clause'
What's the proper way to reference my updated table in the query, if its possible? Also, I want to do this in one SQL statement, not multiple ones.
The problem is that you are using a double subquery and ts.id goes out of the inner query scope, I suspect that you tried to use it for avoiding the error:
You can't specify target table 'ts' for update in FROM clause
but the solution is just omitting the session table from the subquery as you don't need it:
update session set user_id = (select u.id
from training_session_org tso
join organization o on o.id = tso.organization_id
join user u on u.id = o.user_id
where tso.training_session_id=session.id)
In fact, based on the joins you can even simplify the query further:
update session set user_id = (select o.user_id
from training_session_org tso
join organization o on o.id = tso.organization_id
where tso.training_session_id=session.id)
I want to get all the data from the users table & the last record associated with him from my connection_history table , it's working only when i don't add at the end of my query
ORDER BY contributions DESC
( When i add it , i have only the record wich come from users and not the last connection_history record)
My question is : how i can get the entires data ordered by contributions DESC
SELECT * FROM users LEFT JOIN connections_history ch ON users.id = ch.guid
AND EXISTS (SELECT 1
FROM connections_history ch1
WHERE ch.guid = ch1.guid
HAVING Max(ch1.date) = ch.date)
The order by should not affect the results that are returned. It only changes the ordering. You are probably getting what you want, just in an unexpected order. For instance, your query interface might be returning a fixed number of rows. Changing the order of the rows could make it look like the result set is different.
I will say that I find = to be more intuitive than EXISTS for this purpose:
SELECT *
FROM users u LEFT JOIN
connections_history ch
ON u.id = ch.guid AND
ch.date = (SELECT Max(ch1.date)
FROM connections_history ch1
WHERE ch.guid = ch1.guid
)
ORDER BY contributions DESC;
The reason is that the = is directly in the ON clause, so it is clear what the relationship between the tables is.
For your casual consideration, a different formatting of the original code. Note in particular the indented AND suggests the clause is part of the LEFT JOIN, which it is.
SELECT * FROM users
LEFT JOIN connections_history ch ON
users.id = ch.guid
AND EXISTS (SELECT 1
FROM connections_history ch1
WHERE ch.guid = ch1.guid
HAVING Max(ch1.date) = ch.date
)
We can use nested queries to first check for max_date for a given user and pass the list of guid to the nested query assuming all the users has at least one record in the connection history table otherwise you could use Left Join instead.
select B.*,X.* from users B JOIN (
select A.* from connection_history A
where A.guid = B.guid and A.date = (
select max(date) from connection_history where guid = B.guid) )X on
X.guid = B.guid
order by B.contributions DESC;
Below is an sql query that im using to get a list of users who doest have contact details added. The below query seems to work fine. But the only problem is that the image value always returns NULL
I tried to use a sub query to image the image link.Everything works except for the image link.
SELECT a.id,a.name,a.address,a.image_id,(select url
from meta_details b
where b.p_id = 'a.image_id'
and b.meta_val='profile_pic') as image
FROM users
WHERE a.user_id NOT IN (SELECT user_id FROM details where contact_id != '$cid')
Im not sure if this is the correct way, is it possible to make it work to get the image url?
You should remove the quote around a.image_id ( otherwise instead of a join condition on field you have a join on literal value 'a.image_id' that fails because don't match )
SELECT
a.id
,a.name
,a.address
,a.image_id
,(select url
from meta_details b
where b.p_id = a.image_id and b.meta_val='profile_pic' ) as image
FROM users a
WHERE a.user_id NOT IN (SELECT user_id FROM details where contact_id != '$cid')
This type of query can also be expressed with join operations:
select u.*, md.url as image
from users u left join
meta_details md
on md.p_id = u.image_id and
md.meta_val = 'profile_pic' left join
details d
on d.user_id = u.user_id and d.contact_id <> '$cid'
where d.user_id is null;
The advantage to this approach is that the SQL optimizer has a chance of producing a better optimization plan. Also, NOT IN is dangerous because if any rows from the subquery return NULL, then no rows at all will be returned from the query.
Please help me with MySQL update to update the column with result returns from select itself.
For instance, I have two tables
TABLE user(
userid int,
groupid int
)
TABLE thread (
threadid int,
userid int,
sticky tinyint,
vip tinyint
)
Now I'm trying to achieve this with a single update query, but can't seem to do it. What I thought I should do is:
UPDATE user SET groupid=15 WHERE userid IN (SELECT userid FROM thread t LEFT JOIN user u ON u.userid=t.userid WHERE (t.sticky=1 AND t.vip=1) AND (u.groupid=11 OR u.groupid=14) GROUP BY t.userid);
but MySQL saids: #1093 - You can't specify target table 'user' for update in FROM clause
Please help me!
It can be done by generating a new table from left join of two tables and then update from the filtered result, syntax will be as follows:
UPDATE user AS nu
INNER JOIN
(SELECT u.userid, u.groupid
FROM thread t LEFT JOIN user u
ON u.userid=t.userid
WHERE (t.sticky=1 AND t.vip=1) AND
(u.groupid=11 OR u.groupid=14)
GROUP BY t.userid) AS my_table
ON nu.userid = my_table.userid
SET nu.groupid = 15;
Try using the following:
UPDATE user u2 SET u2.groupid=15 WHERE u2.userid IN (SELECT userid FROM thread t LEFT JOIN user u ON u.userid=t.userid WHERE (t.sticky=1 AND t.vip=1) AND (u.groupid=11 OR u.groupid=14) GROUP BY t.userid);
This should do the trick, hope it helps :)
update user1 u
left join thread1 t on t.userid = u.userid
where (t.sticky=1 AND t.vip=1) AND (u.groupid=11 OR u.groupid=14)
set u.groupid = 15
GROUP BY t.userid;
Use this
Is your desired query materially different from this...
UPDATE user u
JOIN thread t
ON t.userid = u.userid
SET groupid = 15
WHERE t.sticky = 1
AND t.vip = 1
AND u.groupid IN(11,14);
?
If so, then as Rockse suggests, consider providin proper DDLs (and/or an sqlfiddle) so that we can more easily replicate the problem, together with a corresponding desired result set.
You can add this before your query:
use yourdatabase;
yourdatabase is database name which includes user table