当前位置:网站首页>MySQL takes the query result as the data updated by update, and concatenates it after the original field data (Lej)
MySQL takes the query result as the data updated by update, and concatenates it after the original field data (Lej)
2022-07-25 09:21:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
update user INNER JOIN
(SELECT GROUP_CONCAT('',id)AS app_id_str FROM `app` WHERE `admin_id` = ' user B Of id' AND `status` > '0' AND `is_deleted` = '0' LIMIT 0,1000 ) app_id_strs
set app_id= CONCAT(app_id,',',app_id_str) WHERE uid= user A Of id;paraphrase :
1. Inquire about app The status in the table is greater than 0, Not delete , And admin_id= user Bid All primary keys of users id, And use (,) comma Concatenate into string aliases app_id_strs ;
(SELECT GROUP_CONCAT('',id)AS app_id_str FROM `app` WHERE `admin_id` = ' user B Of id' AND `status` > '0' AND `is_deleted` = '0' LIMIT 0,1000 ) app_id_strs 2. to update user A Of app_id, stay A Users' original app_id after , use CONCAT, Found on the splicing app_id_strs, And use (,) Comma connection
Expand :
Two 、mysql in update and select Use a combination of
In need update Set parameters from other tables select The result is , Need to put update and select Use a combination of , Different data The library supports different forms , stay mysql Below :
update A inner join(select id,name from B) c on A.id = c.id set A.name = c.name;according to AB Two watches id Same as condition , hold A Tabular name It is amended as follows B Of sql The statement is as shown above
3、 ... and 、update and select Use it together to update data , Case study
There are two tables inspect_danger and company surface , according to company Tabular ID and inspect_danger Tabular COMPANY_ID matching , hold company In table INDUSTRY Update to inspect_danger In the table .
UPDATE inspect_danger ins
LEFT JOIN (
SELECT
com.ID,com.INDUSTRY,ip.ID insId
FROM company com
INNER JOIN inspect_danger ip
ON ip.COMPANY_ID = com.ID
GROUP BY com.ID
) c
ON ins.ID = c.insId
SET ins.COMPANY_INDUSTRY = c.INDUSTRYFour 、 do SQL You often need to , Splice the results of the query into a string .
resolvent : adopt group_concat function
1. Normal query as follows :
select id result from ctp_enum_item limit 100;2. Joining together the results as follows
select group_concat("'",id,"'") result from ctp_enum_item limit 100; 3. Because the result of splicing is very long , The result of splicing is incomplete , It can be solved in the following ways . Execute before each query , Make the query result value larger .
SET SESSION group_concat_max_len = 10240;
perhaps
SET GLOBAL group_concat_max_len = 10240; 4. Add :SQL server Of Splicing SQL as follows :
select stuff((
select ','+ requestid
from nccombinedpayment for xml path('')),1,1,'') as requestid ;5、 ... and 、mysql Splice strings for the queried fields concat
SELECT item_starttime,item_username,CONCAT(' Go out ',item_wcaddress) as item_wcaddress,author from tlk_KQ_goout_app;Before splicing
After splicing
6、 ... and 、 elementary analysis MySQL in concat as well as group_concat Use
7、 ... and 、mysql Append a string to the string of a field in the table
update Table name set Field name =CONCAT( Field name ,” Values to be added ”)WHERE Conditions ;
for example :
update np_order set customer_remark=CONCAT(customer_remark," Values to be added ")WHERE order_id='1'np_order : Table name
customer_remark Field name
where Post specified condition
8、 ... and 、mysql hold select result update Update to table , Update data from query results
Logic : Connect two tables to get finishin The weight of is inserted into sale.
UPDATE sale
INNER JOIN (
SELECT
sale.FNo,
sale.FEntryID,
(finishin.FQty) AS qty
FROM
sale,
finishin
WHERE
sale.FNo = finishin.FNo
AND sale.FEntryID = finishin.FEntryID
ORDER BY
sale.FNo
) sale2 ON sale2.FNo = sale.FNo
AND sale2.FEntryID = sale.FEntryID
SET sale.FqtyIn = sale2.qtyLogic :timefinishin There are multiple identical FBillNo,FEntryID The same data , Sum by grouping qty Total weight , Become a new watch and salesorderinfo Connect tables
UPDATE salesorderinfo
INNER JOIN (
SELECT
salesorderinfo.FBillNo,
salesorderinfo.FEntryID,
(timein.FqtyIn) AS qty
FROM
salesorderinfo,
(
SELECT
*, SUM(timefinishin.FQty) AS FqtyIn
FROM
timefinishin
GROUP BY
FBillNo,
FEntryID
) timein
WHERE
salesorderinfo.FBillNo = timein.FBillNo
AND salesorderinfo.FEntryID = timein.FEntryID
) sale2 ON sale2.FBillNo = salesorderinfo.FBillNo
AND sale2.FEntryID = salesorderinfo.FEntryID
SET salesorderinfo.FqtyIn = sale2.qtyFind something good ,https://httpbin.org/ This website can be used to test http request Reference resources :
https://blog.csdn.net/qq_36823916/article/details/79403696
https://blog.csdn.net/qq5621/article/details/108098826
https://blog.csdn.net/cydbetter/article/details/82117841
https://blog.csdn.net/mary19920410/article/details/76545053
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/111441.html Link to the original text :https://javaforall.cn
边栏推荐
- LabVIEW experiment - temperature detection system (experimental learning version)
- Druid 查询超时配置的探究 → DataSource 和 JdbcTemplate 的 queryTimeout 到底谁生效?
- What are the commands used by pl/sql tools to export SQL files?
- [deep learning] mask Dino Trilogy - the correct way to open Detr Pandora's box
- Silicon Valley class lesson 11 - official account news and wechat authorization
- activemq--异步投递
- 『每日一问』volatile干嘛的
- Redis sentry, master-slave deployment details
- 『怎么用』装饰者模式
- Algorithm --- flip digit (kotlin)
猜你喜欢

LabVIEW experiment - temperature detection system (experimental learning version)

Silicon Valley classroom lesson 12 - official account on demand course and live broadcast management module

Nacos搭建配置中心出现client error: invalid param. endpoint is blank

分享一个避免递归的部门设计方法

mysql中的数据结果排名
![[machine learning] Finally, the important steps of machine learning modeling have been clarified](/img/75/07767ed694502f0c910d35e1447499.jpg)
[machine learning] Finally, the important steps of machine learning modeling have been clarified

Shell script

How to realize the drop-down option box of wechat applet

What version of Oracle10g single instance database is better to upgrade to? Ask for suggestions

2022-7-14 JMeter simulates the login of different users for pressure test
随机推荐
『怎么用』装饰者模式
『怎么用』观察者模式
360 degree drag panorama plug-in tpanorama.js
Nacos搭建配置中心出现client error: invalid param. endpoint is blank
redis的五种数据结构原理分析
Redis-哨兵,主从部署详细篇
activemq--消息重试机制
Network principle (2) -- network development
SQL injection
Activemq-- asynchronous delivery
activemq--可持久化机制之JDBC
Silicon Valley class lesson 11 - official account news and wechat authorization
『每日一问』volatile干嘛的
28.插槽
[arm] Xintang nuc977 transplants wk2124 drive
JS pop-up City filtering component matches mobile terminal
Opencv realizes simple face tracking
activemq--可持久化机制之JDBC代码
什么是单机、集群与分布式?
mysql中的数据结果排名