microsoft sql server的
intersect「返回由intersect運算元的左側和右側的查詢返回的任何不同值」這與標準inner join或where exists查詢不同。
sql server
create table table_a (
id int primary key,
value varchar(255)
create table table_b (
id int primary key,
value varchar(255)
insert into table_a values (1, 'a'), (2, 'b'), (3, 'b');
insert into table_b values (1, 'b');
select value from table_a
intersect
select value from table_b
value
b(1 rows affected)
mysql
create table `table_a` (
`id` int not null auto_increment,
`value` varchar(255),
primary key (`id`)
) engine=innodb;
create table `table_b` like `table_a`;
insert into table_a values (1, 'a'), (2, 'b'), (3, 'b');
insert into table_b values (1, 'b');
select value from table_a
inner join table_b
using (value);
| value |
| b |
| b |
2 rows in set (0.00 sec)
select value from table_a
where (value) in
(select value from table_b);
| value |
| b |
| b |
對於這個特殊的問題,涉及id列,因此不會返回重複的值,但是為了完整性,這裡是乙個mysql替代使用inner join和distinct:
select distinct value from table_a
inner join table_b
using (value);
| value |
| b |
另乙個使用where … in和distinct的例子:
select distinct value from table_a
where (value) in
(select value from table_b);
| value |
| b |
mysql中替代 MySQL中相交的替代方法
microsoft sql server的intersect 返回由intersectoperand左側和右側的查詢返回的任何不同值 這與標準不同inner join或where exists查詢。sqlserver create table table a id int primary key,v...
MySQL遞迴的替代方案
類似查出某個機構下所有的子機構,可用遞迴的方式實現。但mysql不支援遞迴,可以考慮用如下的方式來實現遞迴呼叫。第一種,臨時表方式,使用函式每次查出子機構,再可以和其他表聯查。第二種,新建一張表,列舉出機構和子機構的關係,適合於機構數量不多的情況。第三種,較為通用的情況。使用hierarchy,例如...
mysql 檢查約束 替代 MySQL之檢查約束
定義 mysql檢查約束 check 可以通過 create table 或 alter table 語句實現,根據使用者實際的完整性要求來定義。它可以分別對列或表實施 check 約束。檢查約束使用 check 關鍵字,具體的語法格式如下 check 表示式即為sql 表示式,用於指定需要檢查的限...