the system throw a error "You can't specify target table 'screening' for update in FROM clause"
How do I completed the update like this?
UPDATE screening
SET maileddate = date('Y-m-d', strtotime($mailed_date[$screeningId]))
WHERE user_id IN (SELECT id FROM users
INNER JOIN screening ON
(users.id = screening.users_id
AND screening.id = {$screeningId}))
AND date BETWEEN 05-15/2011 AND 11-15-2011
LIMIT 2
You would have to remove the LIMIT option (similar to a TOP query in other SQL languages). You are basically saying find me the first 2 items and UPDATE. This may produce unexpected results on tables due to sort order.
Try putting the LIMIT clause in your SELECT subquery.
Related
I have a problem with my SQL query. I want to display the last message for a user. So, i use the method "GROUP BY" but he don't display the LAST message.
This is my first query:
SELECT `messages`.*, `conversations`.*
FROM `messages` JOIN `conversations` ON `conversations`.`cNUM` = `messages`.`mCONV_NUM`
WHERE `cUSER2` = '2'
GROUP BY `messages`.`mCONV_NUMn`
I try to follow this subject: ORDER BY date and time BEFORE GROUP BY name in mysql (and a lot of other)
And i have this:
SELECT `messages`.*, `conversations`.*
FROM (SELECT mTIME
FROM `messages`
ORDER BY mTIME desc) AS M
JOIN `conversations` ON `conversations`.`cNUM` = `messages`.`mCONV_NUM`
WHERE `cUSER2` = '2'
GROUP BY `messages`.`mCONV_NUMn`
And i have this error: Table 'messages' unknown.
So.. i need your help guys
You gave an alias M to your messages table, as isaace said you shall refer to it as 'M' in the rest of the query as this temporary name lasts for the whole duration of the query and FROM is an initial phase in the query processing.
We are talking about something called logical query processing, this means that in
your query FROM statement is evaluated and processed initially and
then the rest of query.
In LQP terms queries will be processed in the following order.
FROM --> WHERE --> GROUP BY --> HAVING --> SELECT --> ORDER BY
(Of course I left out some phases but you get the idea)
Also you can use LIMIT to get the last message for a user.
Just add LIMIT 1 at the end.
You say you want the last message for a user, but it seems you really want several messages, namely their last message of each conversation they took part in.
For the lack of CROSS APPLY and window functions in MySQL, you need a query that does a lot of lookups to get the last message per conversation:
select *
from conversations c
join messages m on m.mconv_num = c.cnum
where c.cuser2 = 2
and not exists
(
select *
from messages m2
where m2.mconv_num = m.mconv_num
and m2.mtime > m.mtime
);
I want to display the last message for a user.
You get the one last message for a user with LIMIT:
select *
from conversations c
join messages m on m.mconv_num = c.cnum
where c.cuser2 = 2
order by m.mtime
limit 1;
How can I use a result of select in update query? For example:
Update access_rights
set rfidcode=(Select rfidcode from users where name like 'thomas')
where id_access_rights=3;
This doesn't work. Can anyone help me ?
assuming you want to update a single record your select query needs to return a single result. use the limit keyword.
Update access_rights
set rfidcode=(Select rfidcode from users where name like 'thomas' limit 1)
where id_access_rights=3;
vkp solution if you want to update many related records but you have to watch your joins or you'll get errors, or worse corrupt your data
Update access_rights a, users u
set a.rfidcode = u.rfidcode
where a.userid = u.userid --change this to appropriate join column
and u.name like '%thomas%'
and a.id_access_rights=3;
I have three table one is for users and other one is for subject and third one contain user_id, subject_id foreign keys.
I get unknow coloumn when I run the following sql.
SELECT wp_cons_users.first_name, wp_cons_subject.subject, wp_cons_skilllist.skill_level
FROM `wp_cons_subject`
JOIN wp_cons_skilllist ON wp_cons_skilllist.user_id = wp_cons_users.id
JOIN wp_cons_users ON wp_cons_users.id = wp_cons_skilllist.user_id
WHERE wp_cons_subject.id = '1'
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
I can't find the error with this query.
wp_cons_skilllist
column link to
id (primay)
user_id wp_cons_users -> id
subj_id wp_cons_subject -> id
skill_level
Here I try to get the username, skill level and subject for any given subject id.
Looks like your main problem is with the ordering of your JOINs. In your first join, you are matching with wp_cons_users.id, but you don't join that table until later in the query. If you re-order the joins it should work better. Also, based on your table description, it seems that you will also need to join on subject_id. This query should help:
SELECT wp_cons_users.first_name
, wp_cons_subject.subject
, wp_cons_skilllist.skill_level
FROM wp_cons_users
JOIN `wp_cons_subject`
ON wp_cons_users.id=`wp_cons_subject`.user_id
AND wp_cons_subject.id = '1'
JOIN
wp_cons_skilllist
ON wp_cons_skilllist.user_id = wp_cons_users.id
AND wp_cons_skilllist.subject_id = `wp_cons_subject`.id
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
I am guessing about the field names that weren't in your original query, so you may have to make some changes if they're different from what I'm assuming.
Without information about your attributes in your table, I'm afraid we can only assume that there is no ID column in your wp_cons_users table.
when I corrected the query to following it started to work.
SELECT wp_cons_users.first_name, wp_cons_subject.subject, wp_cons_skilllist.skill_level
FROM `wp_cons_skilllist`
JOIN wp_cons_subject ON wp_cons_subject.id = wp_cons_skilllist.subject_id
JOIN wp_cons_users ON wp_cons_users.id = wp_cons_skilllist.user_id
WHERE wp_cons_skilllist.subject_id = '1'
ORDER BY `wp_cons_skilllist`.`skill_level` DESC
LIMIT 0 , 30
Hi I want to update a table with the values of another but do not how to do it.
I have tried this but it is not working.
UPDATE tblagendamiento SET
CodigoAgenda =
(select tmptable.CodigoCita from tmptable where tmptable.id = tblagendamiento.id);
And this is the error:
Subquery returns more than 1 row
You are actually getting an error because your subquery is returning more than one row. I think you can achieve this simply using an INNER JOIN query
UPDATE tblagendamiento a
INNER JOIN tmptable b
ON a.id = b.id
SET a.CodigoAgenda = b.CodigoCita
The message is telling you that there is more than one row returned by your subquery. Assuming you don't want to use a random value (which you can do by appending limit 1 to the query), it means your where clause is not selective enough.
I have the database of ATM card in which there are fields account_no,card_no,is_blocked,is_activated,issue_date
Fields account number and card numbers are not unique as old card will be expired and marked as is_block=Y and another record with same card number ,account number will be inserted into new row with is_blocked=N . Now i need to update is_blocked/is_activated with help of issue_date i.e
UPDATE card_info set is_blocked='Y' where card_no='6396163270002509'
AND opening_date=(SELECT MAX(opening_date) FROM card_info WHERE card_no='6396163270002509')
but is doesn't allow me to do so
it throws following error
1093 - You can't specify target table 'card_info' for update in FROM clause
Try this instead:
UPDATE card_info ci
INNER JOIN
(
SELECT card_no, MAX(opening_date) MaxOpeningDate
FROM card_info
GROUP BY card_no
) cm ON ci.card_no = cm.card_no AND ci.opening_date = cm.MaxOpeningDate
SET ci.is_blocked='Y'
WHERE ci.card_no = '6396163270002509'
That's one of those stupid limitations of the MySQL parser. The usual way to solve this is to use a JOIN query as Mahmoud has shown.
The (at least to me) surprising part is that it really seems a parser problem, not a problem of the engine itself because if you wrap the sub-select into a derived table, this does work:
UPDATE card_info
SET is_blocked='Y'
WHERE card_no = '6396163270002509'
AND opening_date = ( select max_date
from (
SELECT MAX(opening_date) as_max_date
FROM card_info
WHERE card_no='6396163270002509') t
)