连表查询,distinct过滤重复数据
来源:互联网 发布:数据库审计系统设计 编辑:程序博客网 时间:2024/05/16 19:41
$sql = 'select distinct(g.goods_id),g.*
from lm_goods g, lm_goods_att ga
where g.goods_id = ga.goods_id
';
$sql_count ='SELECT count(distinct(g.goods_id)) c FROM `lm_goods_att` g, `lm_goods_att` ga';
asp多表查询重复、多表查询去重、重复记录的方法。
方法一:用union
select a.menuId, menuAliasNumber, menuName1, menuName2 ,
(select price from workmenuPrice where workmenuPrice.menuId=a.menuId and menuPriceTypeId=1) as 'reg',
(select price from workmenuPrice where workmenuPrice.menuId=a.menuId and menuPriceTypeId=2) as 'large',
(select price from workmenuPrice where workmenuPrice.menuId=a.menuId and menuPriceTypeId=3) as 'small'
from workmenuItems a right join workmenuCatUse b on a.menuId=b.menuId
right join workmenuPrice c on c.menuId=b.menuId
union
select a.menuId, menuAliasNumber, menuName1, menuName2 ,
(select price from workmenuPrice where workmenuPrice.menuId=a.menuId and menuPriceTypeId=1) as 'reg',
(select price from workmenuPrice where workmenuPrice.menuId=a.menuId and menuPriceTypeId=2) as 'large',
(select price from workmenuPrice where workmenuPrice.menuId=a.menuId and menuPriceTypeId=3) as 'small'
from workmenuItems a right join workmenuCatUse b on a.menuId=b.menuId
right join workmenuPrice c on c.menuId=b.menuId
方法二:用distinct
select distinct(a.menuId), menuAliasNumber, menuName1, menuName2 ,
(select price from workmenuPrice where workmenuPrice.menuId=a.menuId and menuPriceTypeId=1) as 'reg',
(select price from workmenuPrice where workmenuPrice.menuId=a.menuId and menuPriceTypeId=2) as 'large',
(select price from workmenuPrice where workmenuPrice.menuId=a.menuId and menuPriceTypeId=3) as 'small'
from workmenuItems a right join workmenuCatUse b on a.menuId=b.menuId
right join workmenuPrice c on c.menuId=b.menuId
方法三:用临时表
四板斧——轻松消除重复记录
殊不知在SQL Server中有一种更为简单的方法,它不需要用游标,只要写一句简单插入语句就能实现删除重复记录的功能。为了能清楚地表述,我们首先假设存在一个产品信息表Products,其表结构如下:
CREATE TABLE Products (
ProductID int,
ProductName nvarchar (40),
Unit char(2),
UnitPrice money
)
表中的数据如图1:
图表
图1中可以看出,产品Chang和Tofu的记录在产品信息表中存在重复。现在要删除这些重复的记录,只保留其中的一条。步骤如下:
第一板斧——建立一张具有相同结构的临时表
CREATE TABLE Products_temp (
ProductID int,
ProductName nvarchar (40),
Unit char(2),
UnitPrice money
)
第二板斧——为该表加上索引,并使其忽略重复的值
方法是在企业管理器中找到上面建立的临时表Products _temp,单击鼠标右键,选择所有任务,选择管理索引,选择新建。如图2所示。
按照图2中圈出来的地方设置索引选项。
图2
第三板斧——拷贝产品信息到临时表
insert into Products_temp Select * from Products
此时SQL Server会返回如下提示:
服务器: 消息 3604,级别 16,状态 1,行 1
已忽略重复的键。
它表明在产品信息临时表Products_temp中不会有重复的行出现。
第四板斧——将新的数据导入原表
将原产品信息表Products清空,并将临时表Products_temp中数据导入,最后删除临时表Products_temp。
delete Products
insert into Products select * from Products_temp
drop table Products_temp
这样就完成了对表中重复记录的删除。无论表有多大,它的执行速度都是相当快的,而且因为几乎不用写语句,所以它也是很安全的。
小提示:上述方法中删除重复记录取决于创建唯一索引时选择的字段,在实际的操作过程中读者务必首先确认创建的唯一索引字段是否正确,以免将有用的数据删除。
- 连表查询,distinct过滤重复数据
- Castle ActiveRecord的distinct过滤重复数据
- MySQL 查询过滤重复数据
- Oracle中通过distinct为什么过滤不了重复的数据?
- 数据库查询时过滤重复数据
- Sql查询语句过滤重复的数据
- mysql查询字段数据不重复distinct及distinct的多字段操作
- C# datatable过滤某一列的重复数据(相当于distinct)
- 手动过滤重复字段数据(因为distinct关键字无法使用。。。)
- 过滤IBatis方法查询出来的重复数据
- Oracle extract 函数及过滤查询到的重复数据
- mysql查询过滤重复数据并随机取出记录
- ibatis 过滤重复数据
- 过滤重复数据
- 过滤数组重复数据
- 添加数据 过滤重复
- django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct
- django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct
- .net 程序反编译工具介绍
- php错误处理、异常!
- 学习velocity的心得
- mysql小数类型字段,float,double
- in嵌套循环会自动过滤掉重复的ID
- 连表查询,distinct过滤重复数据
- INNER JOIN多表联接查询交集查询
- Mysql覆盖索引
- WaitForSingleObject的用法
- mysql 从一个数据库数据表插入到另一个数据库数据表
- MappedbyteBuffer与普通的IO性能比较
- mysql 增加临时列,没有的列
- dedecms数据库类SQL类调试输出执行过的所有SQL语句
- php模拟post提交数据方法