MySQL复杂查询使用临时表/with as(类似表变量)

10 篇文章 0 订阅
6 篇文章 0 订阅

查询需求:
如果第一个SQL能查出结果,则返回结果,否则,执行第二条SQL,返回第二条SQL的结果。

SQL Server中使用表变量的方式:
如果查出有“wangwang”用户则返回,否则查询“zhangzhang”用户的id返回。有优先级

declare @temp table(id int)
insert into @temp
select id
from user
where name = 'wangwang'
if not exists(select id from @temp)
begin
  select id
  from user
  where name='zhangzhang'
end else
begin
  select id from @temp
end

MySQL中没有表变量,只能使用临时表代替:

CREATE TEMPORARY TABLE temp1(id int ); -- 创建临时表1
insert into temp1 
select id
from user  
where name='wangwang'; 

CREATE TEMPORARY TABLE temp2(id int ); -- 创建临时表2
insert into temp2 
select id
from user 
where name='zhangzhang'; 

CREATE TEMPORARY TABLE temp1_re select id from temp1; -- 复制临时表1

select (case when (SELECT count(*) FROM temp1_re)>0 then -- 使用case when判断
 (select id from temp1 order by id desc limit 1) else (
select id from temp2 order by id desc limit 1) end ) id ; 

注意:
(1)需要复制临时表1,是因为“在同一个query语句中,你只能查找一次临时表”,否则报错“can’t reopen ”。针对表中数据不多的情况下,可以复制表进行操作。
(2)因为要在mybatis的xml中执行多条SQL,数据库连接需要设置allowMultiQueries=true。

2019-10-29更新:
发现MySQL有个类似SQLserver中的表变量的语句,那就是with as,也叫做子查询部分(subquery factoring),可以定义一个SQL片断,该SQL片断会被整个SQL语句用到,例如:

with temp_a as(
select id,name from user where name='wangwang'
),
temp_b as(
select id,name from user where name='zhangzhang'
)
select (case when (SELECT count(*) FROM temp_a)>0 then -- 使用case when判断
 			(select id from temp_a order by id desc limit 1) 
 		else 
			(select id from temp_b order by id desc limit 1) 
		end 
	) id 

这种写法是MySQL 8.0的特性
参考:https://www.cnblogs.com/Niko12230/p/5945133.html
https://stackoverflow.com/questions/324935/mysql-with-clause

要实现两张图片的直方图对比,你需要执行以下步骤: 1. 读取两张图片并将它们转换为灰度图像。 2. 将两张图像的灰度值分别统计到两个直方图中。 3. 对两个直方图进行归一化处理,使得它们的和为1。 4. 计算两个直方图之间的距离,可以使用欧几里得距离或者其他的距离度量方法。 5. 根据计算出的距离值,可以判断两张图像的相似度。 下面是一个示例代码,演示如何实现两张图片的直方图对比: ```csharp private void CompareHistograms(string imagePath1, string imagePath2) { // 读取两张图片并将它们转换为灰度图像 Bitmap bmp1 = new Bitmap(imagePath1); Bitmap bmp2 = new Bitmap(imagePath2); Bitmap gray1 = Grayscale(bmp1); Bitmap gray2 = Grayscale(bmp2); // 将两张图像的灰度值分别统计到两个直方图中 int[] hist1 = Histogram(gray1); int[] hist2 = Histogram(gray2); // 对两个直方图进行归一化处理 Normalize(hist1); Normalize(hist2); // 计算两个直方图之间的距离 double distance = Distance(hist1, hist2); // 输出结果 Console.WriteLine("Distance: " + distance); } // 将图片转换为灰度图像 private Bitmap Grayscale(Bitmap bmp) { Bitmap gray = new Bitmap(bmp.Width, bmp.Height); for (int x = 0; x < bmp.Width; x++) { for (int y = 0; y < bmp.Height; y++) { Color color = bmp.GetPixel(x, y); int grayValue = (int)(color.R * 0.299 + color.G * 0.587 + color.B * 0.114); gray.SetPixel(x, y, Color.FromArgb(grayValue, grayValue, grayValue)); } } return gray; } // 计算直方图 private int[] Histogram(Bitmap bmp) { int[] hist = new int[256]; for (int x = 0; x < bmp.Width; x++) { for (int y = 0; y < bmp.Height; y++) { Color color = bmp.GetPixel(x, y); int grayValue = color.R; hist[grayValue]++; } } return hist; } // 归一化直方图 private void Normalize(int[] hist) { int sum = 0; for (int i = 0; i < hist.Length; i++) { sum += hist[i]; } for (int i = 0; i < hist.Length; i++) { hist[i] = (int)(hist[i] * 1.0 / sum * 100); } } // 计算直方图距离 private double Distance(int[] hist1, int[] hist2) { double distance = 0; for (int i = 0; i < hist1.Length; i++) { distance += Math.Pow(hist1[i] - hist2[i], 2); } distance = Math.Sqrt(distance); return distance; } ``` 在上面的示例代码中,Grayscale方法将一张彩色图像转换为灰度图像,Histogram方法计算灰度直方图,Normalize方法对直方图进行归一化处理,Distance方法计算两个直方图之间的距离。最后,你可以调用CompareHistograms方法,传入两张图片的路径,即可计算它们的直方图距离并输出结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值