6.12. SQL 92

简介:


insert + select

insert into product_type_commission select id,5,1,1,0,0,0,0,0,0 from product_type where title='notebook' and is_physical=0;
			

update table1,table2

begin;
ALTER TABLE `customer` ADD COLUMN `cutoff_time` TIMESTAMP NOT NULL default '0000-00-00 00:00:00';
update customer,agent set customer.cutoff_time = agent.cutoff_time where customer.id = agent.id;
ALTER TABLE `agent` DROP COLUMN `cutoff_time`;
commit;
			

update table1 set field1 = (select value from table2)

UPDATE
	transaction
SET
	transaction.total_sold_price = (
		SELECT
			SUM(transaction_item.price)
	FROM
			transaction_item
		WHERE transaction_item.transaction_id = 100
	)
WHERE
	transaction.id = 100
			

update table1, (select * from other) as table2 set table1.field1 = table2.field1

UPDATE
	transaction,(	SELECT
						SUM(product_item.bought_price) AS total_bought_price, transaction_item.transaction_id
					FROM
						transaction_item
			    	WHERE
						transaction_item.transaction_id IN ( '123','456' )
			     ) as total
SET
	transaction.total_bought_price = total.total_bought_price
WHERE
	transaction.id = total.transaction_id

			

join + subquery

select u.*,t.category,t.items,t.[property] from tb_sysregchkusers as u left join (select a.items as category, b.* from (select id, items from tb_sysregchktask where categoryid=0) as a left join tb_sysregchktask as b on b.categoryid=a.id ) as t on u.taskID=t.id

select * from tb_sysregchklog where CONVERT(datetime,CONVERT(varchar(10),checkTime,120)) between convert(datetime,'2007-12-12') and convert(datetime,'2007-12-12')
		
select DISTINCT user_point_history.user_id,user.username,
(select count(id) from transaction where id = user_point_history.transaction_id) as transactions,
(SELECT SUM(u_p_h.points) FROM user_point_history as u_p_h WHERE u_p_h.type != 'RDMP' AND u_p_h.status IN('pr','ac') AND u_p_h.user_id = user_point_history.user_id) as total_points_earned,
(SELECT SUM(u_p_h.points) FROM user_point_history as u_p_h WHERE u_p_h.type = 'RDMP' AND u_p_h.status IN('pr','ac') AND u_p_h.user_id = user_point_history.user_id) as total_points_redeemed
from user_point_history,user where user_point_history.user_id = user.id;
		

(total_points_earned - total_points_redeemed) as current_balance_points

select user_id, username, transactions, total_points_earned, total_points_redeemed, (total_points_earned - total_points_redeemed) as current_balance_points
from (select DISTINCT user_point_history.user_id,user.username,
(select count(id) from transaction where id = user_point_history.transaction_id) as transactions,
(SELECT SUM(u_p_h.points) FROM user_point_history as u_p_h WHERE u_p_h.type != 'RDMP' AND u_p_h.status IN('pr','ac') AND u_p_h.user_id = user_point_history.user_id) as total_points_earned,
(SELECT SUM(u_p_h.points) FROM user_point_history as u_p_h WHERE u_p_h.type = 'RDMP' AND u_p_h.status IN('pr','ac') AND u_p_h.user_id = user_point_history.user_id) as total_points_redeemed
from user_point_history,user where user_point_history.user_id = user.id) as user_performance;
		

subquery作为一个字段使用

select product_type_attribute.*,(select 'selected' from product_type_attribute_set where product_type_attribute_set.product_type_attribute_id = product_type_attribute.id and product_type_attribute_set.product_type_id = 26) as selected
from product_type_attribute;
		




原文出处:Netkiller 系列 手札
本文作者:陈景峯
转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。

目录
相关文章
|
6月前
|
SQL 存储 数据管理
SQL
SQL
40 0
|
3月前
|
SQL 数据库 开发者
SQL中为什么不要使用1=1?
【8月更文挑战第11天】在SQL查询语句中,偶尔会遇到使用1=1作为WHERE子句一部分的情况,这种做法看似无害,实则隐藏着一些潜在的问题和更好的替代方案。本文将深入探讨为什么不建议在SQL中使用1=1,并分享更优化的查询构建策略。
83 2
|
4月前
|
SQL 存储 大数据
SQL技巧
【7月更文挑战第26天】SQL技巧
21 1
|
5月前
|
SQL Java 数据库连接
SQL中为什么不要使用1=1
本文探讨了在SQL查询中使用`1=1`的现象及其背后的原因与问题。开发人员有时使用`1=1`作为始终为真的条件来方便动态构建SQL语句,但这样做可能会带来性能问题,尽管现代数据库查询优化器可能能优化掉这种条件,但在复杂查询或特定系统中仍可能影响效率。此外,`1=1`还降低了代码的可读性和跨数据库的兼容性。建议使用更佳实践,如MyBatis的动态SQL标签或Entity Framework的函数式查询,以避免不必要的条件。代码质量的重要性在于每一行代码都应有其明确的目的,避免浪费计算资源。
|
SQL Oracle 关系型数据库
sql
sql
92 0
|
SQL 程序员 数据库
sql 总结
一对多:在多的表中添加建立关系的字段(外键)指向另外一张表。如果需要查询一张表的全部和另外一张表的交集时,使用外连接,连表查询(左外连接)(显示左表的全部信息和右表相关联的信。连表查询(右外连接)(显示右表的全部信息和左表相关联的信。等值连接和内连接查询的是两个表的交集数据,推荐使用内连接。:选择插入必须选择需要插入的字段,选择对应字段的值,批量。查询所有部门的名称,地点和对应的员工姓名和工资。等值连接和内连接查询到的都是两张表的交集数据。外连接查询的是一张表的全部和另外一张表的交集。
131 0
|
关系型数据库
|
SQL
xttcnvrtbkupdest.sql
---- Convert the incremental backup (target convert) -- Inputs: cross-plaform backups set serveroutput on;set termout on;set verify off; DE...
851 0
xttstartupnomount.sql
connect / as sysdba; startup force nomount; exit;
723 0