UPDATE all, except last - mysql

I have a table like this:
table_documents
document_id
document_folder_id
document_title
document_notify_expired
ID FOLDER TITLE Notify Expired
1 2 Test1 1
2 2 Test2 1
3 2 Test3 1
4 2 Test4 1
5 2 Test5 1
I'm like to UPDATE and set document_notify_expired to 0 for all records EXCEPT last, for a specific folder like below
ID FOLDER TITLE Notify Expired
1 2 Test1 0
2 2 Test2 0
3 2 Test3 0
4 2 Test4 0
5 2 Test5 1
Here my code but not update as expected
UPDATE table_documents docs
LEFT OUTER JOIN ( SELECT * FROM table_documents ORDER BY document_id DESC LIMIT 1 )last_doc ON last_doc.document_id = docs.document_id
SET doc.document_notify_expired = '0'
WHERE document_folder_id = '2'
AND last_doc.document_notify_expired = '1'

Try this out
UPDATE table_documents docs
INNER JOIN
(SELECT
MAX(id) id
FROM
table_documents) docsmax ON docs.id != docsmax.id
SET
document_notify_expired = 0;
Obviously the last row has the greatest id, so this row is not going to be there after the join, which will returns all the other rows and you can play with them as you wish.

update table1.table_documents
set table1.document_notify_expired = 0
where table1.document_folder_id = 2
and not table1.document_id = (
select table2.document_id
from table_documents as table2
where table2.document_folder_id = 2
order by table2.document_id desc
limit 1
);

Related

How to do the minus operation given 2 columns using mysql

allpairs
user1 user2
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
3 3
likesPairs
user1 user2
1 2
2 1
3 1
I want to do allPairs - likedPairs to get the relation
notliked
user1 user2
1 1
2 2
3 2
1 3
2 3
3 3
I tried something like this but it just errors
select user1, user2
from allpairs NOT IN likespairs
This sounds like not exists:
select ap.*
from allpairs ap
where not exists (
select 1
from likespairs lp
where lp.user1 = ap.user1 and lp.user2 = ap.user2
)
In this case the LEFT JOIN can be used with check for NULL values:
select allpairs.*
from allpairs
left join likespairs
on (allpairs.user1 = likespairs.user1 and allpairs.user2 = likespairs.user2)
where likespairs.user1 is null;
Here you can try the SQL code
If pair's order not a matter you can add next into LEFT JOIN condition:
select allpairs.*
from allpairs
left join likespairs on (
(allpairs.user1 = likespairs.user1 and allpairs.user2 = likespairs.user2) or
(allpairs.user2 = likespairs.user1 and allpairs.user1 = likespairs.user2)
)
where likespairs.user1 is null;
Try SQL here

count client_id from tbl_appointment_book using join in php

I have two table. I want count client_id from tbl_appointment_book but tbl_appointment_book's id and tbl_appointment_service's appointment_id should be match.
tbl_appointment_book
id appointment_date client_id status
1 2016-05-11 1 1
2 2016-05-12 1 1
tbl_appointment_service
id appointment_id service_id team_id
1 1 1 1
2 1 2 1
3 1 8 5
4 2 1 1
5 2 1 2
I want to count client_id from tbl_appointment_book condition
appointment_date=2016-05-11, team_id=1
You can use JOIN to achieve your wanted result:
SELECT count(a.client_id) FROM tbl_appointment_book a
JOIN tbl_appointment_service b
ON a.id = b.appointment_id
WHERE a.appointment_date = '2016-05-11' AND b.team_id = 1
Result will be the number of rows returned.
Try the below query, it should work fine.
SELECT COUNT (tab.client_id)
FROM tbl_appointment_book tab
JOIN tbl_appointment_service tas
ON tab.id = tas.appointment_id
WHERE tab.appointment_date = '2016-05-11' AND tas.team_id = 1;

Mysql statement, if at least one row contains result

i've a table with records:
----------------------------------
ID | UniqueId | Name | Result
----------------------------------
1 1 Test1 OK
2 1 Test1 Cancelled
3 1 Test1 OK
4 2 Test2 OK
5 2 Test2 OK
6 2 Test2 OK
7 2 Test2 OK
8 3 Test3 OK
9 3 Test3 OK
Let's say i wan't to check if at least one row with UniqueId = 1 not contains Result == Cancelled. To exclude record with UniqueId = 1, because it is cancelled.
How can i do this?
Thank you
SELECT t1.* from table as t1 where t1.UniqueId not in(select t.UniqueId from table as t
where t.Result="Cancelled")
Just ask for rows with UniqueId = 1 and Result != Cancelled
SELECT ID FROM table WHERE UniqueId = 1 AND Result <> 'Cancelled' LIMIT 1
I'd go this way, but the other answers are a bit easier in terms of syntax.
SELECT UniqueId, Name
FROM Table T
GROUP BY UniqueId, Name
HAVING Result != 'Cancelled'
SELECT id FROM mytable WHERE uniqueId = 1 AND result != 'Cancelled'

update from select result

I tried to update the color in table tbl of colors in the item table based on field item_id, with the following query. but I get an error Sql error (1242).
update tbl mt
left join tbl_detail dt on mt.tbl_No = dt.tbl_No
set dt.color_Id =
(
select p.color_ID
from ( select dt.item_Id, dt.color_Id
from tbl mt
left join tbl_detail dt on mt.tbl_No = dt.tbl_No
where mt.Tipe = 2 ) h
left join itemp on h.item_id = p.item_id
)
where mt.Tipe = 2 ;
I want to update a field color in the table tbl with existing color in the item table. How I should make it?
I have tried a simple syntax like this but then I get sql error (1442)
update tbl mt
left join tbl_detail dt on mt.tbl_No = dt.tbl_No
left join item p on dt.item_Id = p.item_Id
set dt.color_Id = p.color_id
where mt.Tipe = 2 ;
Example :
table tbl:
|tbl_No|tipe|
1 2
2 2
3 2
table tbl_detail:
|tbl_detail_No|tbl_No|item_id|color_id|
1 1 1 null
2 1 2 null
1 2 3 null
2 2 4 null
table item:
|item_id|color_id|
1 1
2 2
3 3
4 4
I want to update color_id in table tbl_detail based on item_id in table item; after the update, the table tbl_detail should be as follows.
table tbl_detail:
|tbl_detail_No|tbl_No|item_id|color_id|
1 1 1 1
2 1 2 2
1 2 3 3
2 2 4 4
(oh, I'm really bad at explaining it, hope someone can understand as English is not my first language.)
Thanks for all the help and advice.

Mysql help updating rows with same value

I have a table named status_t:
**id** **page** **status** **locked**_by
1 0 2 0
1 1 2 0
1 2 2 0
2 0 0 0
2 1 2 0
2 2 2 0
primary key is ( id, page) .
In the above example I would like to update all the
rows that all pages have status = 2.
i.e. the query should update all the rows with id = 1 to status 3. So the table will become
**id** **page** **status** **locked**_by
1 0 3 1
1 1 3 1
1 2 3 1
2 0 0 0
2 1 2 0
2 2 2 0
I have tried:
SELECT * FROM status_t AS t
WHERE id IN
(SELECT id FROM status WHERE status = 0) LIMIT 10
the above query fetches the rows to be updated but I cannot do that:
UPDATE status_t as S1 WHERE id IN
(SELECT id FROM status_t WHERE status = 2)
SET S1.status = 3, S1.locked_by = 1
EDIT:
THE ABOVE TABLE IS JUST AN EXAMPLE.
I do not want to update WHERE id = 1 . I just want to update rows no matter the id
that have status = 2 for the same id.
In the above example if the row with key (2, 2) had status = 2 then it should be updated.
This should work:
update status_t t
join (
select distinct id from status_t s
where status=2 and not exists (
select 1 from status_t ss
where s.id=ss.id and s.status <> ss.status
)
) i on i.id = t.id
set t.status = 3
The inner query selects IDs where all values of status are set to 2. It achieves this result by checking that tere are no rows with the same ID and a different status.
Here is a demo on sqlfiddle.
Try this:
UPDATE status_t a
INNER JOIN (SELECT id FROM status_t WHERE STATUS = 2 GROUP BY id HAVING COUNT(*) = 3 LIMIT 1) AS b ON a.id = b.id
SET a.status = 3, a.locked_by = 1;
This will update data as you want for all pages have status = 2
The following should do the trick:
UPDATE status_t
SET status = 3, locked_by = 1
WHERE status = 2
There should be no need to use a sub-query, as the WHERE should be able to be used directly in the UPDATE statement.
EDIT:
I just noticed that you only had an update against the rows with ID = 1, which does not match the statement 'In the above example I would like to update all the rows that all pages have status = 2.. The rows whereid = 2, andstatus = 2` would also be updated by my query.
If you need to only update a particular id, add the following at the end of my query:
AND id = 1
try this
update status_t set staus=3 ,locked_by=1
where id=1
or
update status_t set status=3 ,locked_by=1
where status=2
UPDATE status_t SET status = 3, S1.locked_by = 1 WHERE id = 1
Try this::
UPDATE status_t SET status = 3, locked_by = 1 WHERE status = 2
And if you want to achieve this in your way :
UPDATE
status_t as S1
INNER JOIN
(
SELECT keyword_id
FROM status_t WHERE status = 2
) as tempStatus ON id= tempStatus.keyword_id
SET S1.status = 3, S1.locked_by = 1