说我在下面的表Table1中有一些代码。
Column1 | Column2
41391175 | 41000
41523664 | 41523
110505116 | 110509
110453629 | 110505
41000 | 351592
Column1和Column2为NVARCHAR(10)
。我要返回的是第1列中的所有条目,其中Column2在IN Column1中,所以我希望结果看起来像这样-
Column1
41523664
110505116
41000
现在,这就是我的代码的样子-
SELECT Column1
FROM Table1
INTERSECT
SELECT Column2
FROM Table1 ;
但是截至目前,它只能找到与Column2中完全相同的数字,而不是Column1包含在其中的数字,所以我只会得到这个结果-
Column1
41000
是否有解决此问题的方法,或在INTERSECT中获得IN子句?经过研究后,我一直找不到能做到这一点的东西。
答案 0 :(得分:1)
我想您可以使用LIKE运算符来匹配它们:
# rake db:migrate rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:
class CreateUsers < ActiveRecord::Migration[4.2]
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:528:in `inherited'
/root/genieacs-gui/db/migrate/20160202145722_create_users.rb:1:in `<top (required)>'
/var/lib/gems/2.3.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
/var/lib/gems/2.3.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `block in require'
/var/lib/gems/2.3.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:253:in `load_dependency'
/var/lib/gems/2.3.0/gems/activesupport-5.2.1/lib/active_support/dependencies.rb:287:in `require'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:986:in `load_migration'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:982:in `migration'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:977:in `disable_ddl_transaction'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:1350:in `use_transaction?'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:1342:in `ddl_transaction'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:1291:in `execute_migration_in_transaction'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:1263:in `block in migrate_without_lock'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:1262:in `each'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:1262:in `migrate_without_lock'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:1212:in `migrate'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:1036:in `up'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/migration.rb:1011:in `migrate'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/tasks/database_tasks.rb:172:in `migrate'
/var/lib/gems/2.3.0/gems/activerecord-5.2.1/lib/active_record/railties/databases.rake:60:in `block (2 levels) in <top (required)>'
/var/lib/gems/2.3.0/gems/rake-12.3.1/exe/rake:27:in `<top (required)>'
#...
Tasks: TOP => db:migrate
答案 1 :(得分:0)
select c1.column1 from table1 c1 inner join (select distinct column2 from table1) c2 on charindex(c2.column2, c1.column1) = 1