I have a table like this:
+----------------------------+
| id | name | helper |
+----------------------------+
| 1 | user1 | NULL |
| 2 | user1 | NULL |
| 3 | user1 | NULL |
| 4 | user2 | NULL |
| 5 | user2 | NULL |
+----------------------------+
Now I want to update the LAST occurrence of "user1" - how do I do that?
This is my example query right now:
UPDATE Table SET helper = 'bob' WHERE name = 'user1' AND helper IS NULL;
However, this updates all the "user1" Entries.
Thanks in advance.
update t set helper = 'bob' where name = 'user1' order by id desc limit 1;
see it working live in an sqlfiddle
This should work.
UPDATE Table SET helper = 'bob' WHERE id = (SELECT MAX(id) FROM Table WHERE name = 'user1')
Just use MAX(id) to get the last row of 'user1'.
Related
I have a table like this:
// users
+---------+------------+
| user_id | reputation |
+---------+------------+
| 1 | null |
| 2 | null |
+---------+------------+
Also I have an array like this: [1 => 15, 2 => 83].
I can parse that array and restruct it to any case needed. I want to know how can I update that value to that table like this:
// users - expected result
+---------+------------+
| user_id | reputation |
+---------+------------+
| 1 | 15 |
| 2 | 83 |
+---------+------------+
An idea how can I do that?
I want a query similar to this:
update users u
join ( ... ) as temp
on u.user_id = temp.user_id
set u.reputation = temp.rep
Here's a sample of the data I'm pulling from:
+-----+------------+---------+----------+------------+
| id | name | carrier | tracking | date |
+-----+------------+---------+----------+------------+
| 123 | john smith | UPS | abcdefgh | 2018-06-22 |
| 123 | john smith | USPS | 12345678 | 2018-06-23 |
+-----+------------+---------+----------+------------+
The table I'm updating to only has one record for each ID (whereas the table I'm pulling from can have multiple), and I'm trying to get the final output to look like this:
+-----+------------------------------------------+
| id | shipping_info |
+-----+------------------------------------------|
| 123 | 6/22 - UPS abcdefgh 6/23 - USPS 12345678 |
+-----+------------------------------------------+
I have a query and I'm almost there:
update table1
set shipping_info = concat(
DATE_FORMAT(table2.date_time, '\n%c/%e - '),
table2.carrier,
table2.tracking)
where id = '123'
+-----+---------------------+
| id | shipping_info |
+-----+---------------------|
| 123 | 6/22 - UPS abcdefgh |
+-----+---------------------+
So my problem is, when I run the update it only updates the table with the first row of data, but I need to append it with the second row as well.
you should group_concat the concatenated string eg:
select id, group_concat(concat(date,'-' , carrier,'-', tracking))
from my_table
group by id
and for update
update my_update_table m
inner join (
select id, group_concat(concat(date,'-' , carrier,'-', tracking)) my_col
from my_table
group by id
) t on m.id = t.id
set m.shipping_info = t.my_col
I would like to insert data in MySQL with automatic naming on field username but how i can do it?.
Currently data at table is:
+----+----------+
| id | username |
+----+----------+
| 1 | admin1 |
| 2 | admin2 |
+----+----------+
I try using this sql but it's can't:
INSERT INTO `tbl_user` (
`username`
)
VALUES (
CONCAT('admin',(SELECT MAX(SUBSTRING_INDEX(`username`,'admin',-1))+1 FROM `tbl_user`))
);
and get error message #1093 - You can't specify target table 'tbl_user' for update in FROM clause
Final result i want is:
+----+----------+
| id | username |
+----+----------+
| 1 | admin1 |
| 2 | admin2 |
| 6 | admin3 |
| 9 | admin4 |
+----+----------+
is that possible? thanks.
You can use a trigger that would update the column username after an insert. Here's some more information on how to actually do this: http://www.roseindia.net/sql/trigger/mysql-trigger-after-insert.shtml
Edit
I forgot that MySQL won't allow you to update a table from a trigger declared on the same table.
However, this should do what you're trying to do:
SET #id := (SELECT id FROM YOUR_TABLE ORDER BY id DESC LIMIT 1);
INSERT INTO YOUR_TABLE (username) VALUES(
CONCAT("ADMIN", #id + 1)
);
Query:
SQLFIDDLEExample
INSERT INTO `tbl_user` (
`username`
)
VALUES (
CONCAT('admin',(SELECT MAX(CAST(REPLACE(`username`,'admin','') AS SIGNED INTEGER))+1
FROM (SELECT * FROM tbl_user) t))
);
Result:
| ID | USERNAME |
---------------------
| 1 | admin1 |
| 2 | admin2 |
| (null) | admin3 |
if've got three tables in my MYSQL Database and want to connect two of this table with one table. The tables look like this
CONNECTTABLE
+----+-----------+---------+
| ID | search_id | room_id |
+----+-----------+---------+
SEARCHTABLE
+----+-----------+-----+
| ID | search_id | ... |
+----+-----------+-----+
SEARCHTABLE
+----+---------+-----+
| ID | room_id | ... |
+----+---------+-----+
Is it possible to ensure via MYSQL that in the CONNECTTABLE only search_id OR room_id is not null per datarow? If I can do so, how can I do so?
Valid rows:
+----+-----------+---------+
| ID | search_id | room_id |
+----+-----------+---------+
| 1 | 42 | NULL |
+----+-----------+---------+
| 2 | NULL | 1337 |
+----+-----------+---------+
Invalid row:
+----+-----------+---------+
| ID | search_id | room_id |
+----+-----------+---------+
| 3 | 42 | 17 |
+----+-----------+---------+
Best regards,
Gerrit
What you want is called a "CHECK constraint". Unfortunately, MySQL does not support them. Well, it will let you define them, but it won't actually check them. This article discusses using triggers to implement the same functionality.
hope this helps
select * from tableName where search_id is null && room_id is not null
union
select * from tableName where search_id is not null && room_id is null
First of all, it is not possible to add two tables with the same name in the database. But i presume that this are only dummy tablenames right? try this one:
SELECT a.ID, b.Search_ID, c.Room_ID
FROM ConnectTable a LEFT JOIN SearchTableA b
ON a.ID = b.ID
LEFT JOIN SearchTableB c
ON a.ID = c.ID
WHERE b.ID IS NULL OR
c.ID IS NULL
I am having problems selecting items from a table where a device_id can be either in the from_device_id column or the to_device_id column. I am trying to return all chats where the given device is ID is in the from_device_id or to_device_id columns, but only return the latest message.
select chat.*, (select screen_name from usr where chat.from_device_id=usr.device_id limit 1) as from_screen_name, (select screen_name from usr where chat.to_device_id=usr.device_id limit 1) as to_screen_name from chat where to_device_id="ffffffff-af28-3427-a2bc-83865900edbe" or from_device_id="ffffffff-af28-3427-a2bc-83865900edbe" group by from_device_id, to_device_id;
+----+--------------------------------------+--------------------------------------+---------+---------------------+------------------+----------------+
| id | from_device_id | to_device_id | message | date | from_screen_name | to_screen_name |
+----+--------------------------------------+--------------------------------------+---------+---------------------+------------------+----------------+
| 20 | ffffffff-af28-3427-a2bc-83860033c587 | ffffffff-af28-3427-a2bc-83865900edbe | ee | 2011-02-28 12:36:38 | kevin | handset |
| 1 | ffffffff-af28-3427-a2bc-83865900edbe | ffffffff-af28-3427-a2bc-83860033c587 | yyy | 2011-02-27 17:43:17 | handset | kevin |
+----+--------------------------------------+--------------------------------------+---------+---------------------+------------------+----------------+
2 rows in set (0.00 sec)
As expected, two rows are returned. How can I modify this query to only return one row?
mysql> describe chat;
+----------------+---------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+---------------+------+-----+-------------------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| from_device_id | varchar(128) | NO | | NULL | |
| to_device_id | varchar(128) | NO | | NULL | |
| message | varchar(2048) | NO | | NULL | |
| date | timestamp | YES | | CURRENT_TIMESTAMP | |
+----------------+---------------+------+-----+-------------------+----------------+
5 rows in set (0.00 sec)
select chat.*,
(select screen_name
from usr
where chat.from_device_id=usr.device_id
limit 1
) as from_screen_name,
(select screen_name
from usr
where chat.to_device_id=usr.device_id
limit 1
) as to_screen_name
from chat
where to_device_id="ffffffff-af28-3427-a2bc-83865900edbe" or
from_device_id="ffffffff-af28-3427-a2bc-83865900edbe"
group by from_device_id, to_device_id
order by date DESC
limit 1;
You need to tell SQL that it should sort the returned data by date to get the most recent chat. Then you just limit the returned rows to 1.
You shouldn't need to use a Group By at all. Rather, you can simply use the Limit predicate to return the last row. In addition, you shouldn't need subqueries as you can use simply Joins. If chat.from_device_id and chat.to_device_id are both not-nullable, then you can replace the Left Joins with Inner Joins.
Select chat.id
, chat.from_device_id
, chat.to_device_id
, chat.message
, chat.date
, FromUser.screen_name As from_screen_nam
, ToUser.screen_name As to_screen_name
From chat
Left Join usr As FromUser
On FromUser.device_id = chat.from_device_id
Left Join usr As ToUser
On ToUser.device_id = chat.to_device_id
Where chat.to_device_id="ffffffff-af28-3427-a2bc-83865900edbe"
Or chat.from_device_id="ffffffff-af28-3427-a2bc-83865900edbe"
Order By chat.date Desc
Limit 1