以下是我的表结构的详细信息。
表结构“部门”:
id | department
1 | Department 1
2 | Department 2
3 | Department 3
4 | Department 4
表结构“custom_forms_departments”:
id | form_id | department_id | enduser_to_department | department_to_enduser
1 | 5 | 1 | Y | N
2 | 6 | 1 | N | Y
3 | 8 | 2 | Y | Y
4 | 7 | 3 | N | Y
5 | 4 | 3 | Y | N
6 | 2 | 4 | N | N
结果应返回 department_id,其在同一行或不同行中的字段“enduser_to_department”和“department_to_enduser”的值为“Y”。
department_id = 1 contatin value "Y"for "enduser_to_department"and "department_to_enduser"in different rows department_id = 2 contatin value "Y"for "enduser_to_department"and "department_to_enduser"in the same rows department_id = 3 contatin value "Y"for "enduser_to_department"and "department_to_enduser"in different rows
结果:
department_id | departments
1 | Department 1
2 | Department 2
3 | Deapartment 3
我正在使用以下未给出正确结果的 SQL 查询。
SELECT departments.department_id, departments.department
FROM custom_forms_departments , departments
WHERE departments.department_id = custom_forms_departments.department_id
AND (custom_forms_departments.enduser_to_department = 'Y'
OR custom_forms_departments.department_to_enduser = 'Y')
GROUP BY departments.department_id
ORDER BY departments.department_id DESC
请给我推荐这个。
最佳答案
首先尝试处理custom_forms_departments。
方法:创建 2 个 custom_forms_departments 副本(c1 和 c2)。你会想加入他们,基于 (c1.department_id = c2.department_id) - 简单,并且 (c1.enduser_to_department = c2.department_to_enduser) - 因为你想要只获取同时包含“Y”的行(将过滤 WHEN 中的“Y”,但现在,您将获得在两列中具有相同值的任何行)。其次,使用 WHEN 仅过滤“Y”。
SELECT
custom_forms_departments.department_id
FROM
custom_forms_departments c1
INNER JOIN
custom_forms_departments c2 ON c1.department_id = c2.department_id
AND c1.enduser_to_department = c2.department_to_enduser
WHERE
c1.enduser_to_department = 'Y'
GROUP BY
c1.department_id
;
现在我们有了“复杂”的员工,让我们把所有人员聚集在一起并添加departments 列:
SELECT
departments.department_id, departments.department
FROM
departments
INNER JOIN
(SELECT
custom_forms_departments.department_id
FROM
custom_forms_departments c1
INNER JOIN custom_forms_departments c2 ON c1.department_id = c2.department_id
AND c1.enduser_to_department = c2.department_to_enduser
WHERE
c1.enduser_to_department = 'Y'
GROUP BY
c1.department_id) c3 ON departments.department_id = c3.department_id
;
关于mysql查询获取id不同列的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30388466/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
Sinatra新手;我正在运行一些rspec测试,但在日志中收到了一堆不需要的噪音。如何消除日志中过多的噪音?我仔细检查了环境是否设置为:test,这意味着记录器级别应设置为WARN而不是DEBUG。spec_helper:require"./app"require"sinatra"require"rspec"require"rack/test"require"database_cleaner"require"factory_girl"set:environment,:testFactoryGirl.definition_file_paths=%w{./factories./test/
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur