首页 \ 问答 \ 形成复杂的SQL语句(Forming a complex SQL statement)

形成复杂的SQL语句(Forming a complex SQL statement)

我正忙着为我的一个项目构建一个有效的SQL语句。 首先,我有一个“问题”表和一个“响应”表。 响应表通过名为“question_id”的外部ID(与问题“id”相关联)与问题表相关联。 一个问题不一定得有回应,但我能提出的唯一可行的陈述只会提出一个有回应的问题,但我需要它来表明每个问题是否有回应。

那个SQL语句是:

SELECT u.firstname, q.question, r.tutor_id, r.response FROM response r 
JOIN question q ON q.id = r.question_id
JOIN user u ON u.id = q.user_id

我的另一个问题是我也试图拉出导师的名字,但只能拉“tutor_id”,所以任何帮助都会很棒。 如果有人有任何提示,我会很感激!

适用于:

SELECT u.firstname, q.question, v.firstname, r.response
FROM question q
INNER JOIN user u ON u.id = q.user_di
LEFT JOIN response r ON q.id = r.question_id
LEFT JOIN user v ON v.id = r.tutor_id

I'm coming up stumped on forming a working SQL statement for a project of mine. First of all, I have a "questions" table and a "response" table. The response table is tied to the question table through a foreign id called "question_id" (tied to the questions "id"). A question doesn't necessarily have to have a response, but the only working statement I can come up with will only pull a question that has a response, but I need it to show every question whether or not there is a response.

That SQL statement is:

SELECT u.firstname, q.question, r.tutor_id, r.response FROM response r 
JOIN question q ON q.id = r.question_id
JOIN user u ON u.id = q.user_id

My other problem is that I'm also trying to pull the firstname of the tutor, but can only pull the "tutor_id", so any help with that would also be awesome. If anyone has any tips, I'd appreciate it!

Works with:

SELECT u.firstname, q.question, v.firstname, r.response
FROM question q
INNER JOIN user u ON u.id = q.user_di
LEFT JOIN response r ON q.id = r.question_id
LEFT JOIN user v ON v.id = r.tutor_id

原文:https://stackoverflow.com/questions/3315130
更新时间:2023-01-29 16:01

最满意答案

您的测试没有失败的原因是您的onComplete块正在与您的实际测试方法不同的线程中运行。 考虑这个测试:

class MyTest extends FlatSpec {
    it should "get a List" in {
        def fs: List[Future[Int]] = List(Future(1))

        println("Test thread " + Thread.currentThread().getName)

        val f: Future[List[Int]] = Future.sequence(fs)
        f onComplete { case x => {
            println("Complete thread " + Thread.currentThread().getName)
            assert(x.get == List(5))
        }}
        Await.ready(f, Duration.Inf)
   }
}

运行时,我们得到此输出,测试不会失败:

Test thread: ScalaTest-run-running-MyTest
Complete thread: ForkJoinPool-1-worker-11

您希望断言与测试位于同一个线程中。 最终我认为你在onComplete之外寻找这样的东西:

assert(Await.result(f, 5 seconds) == List(1, 2, 3))

The reason your test isn't failing is that your onComplete block is being run in a separate thread from your actual test method. Consider this test:

class MyTest extends FlatSpec {
    it should "get a List" in {
        def fs: List[Future[Int]] = List(Future(1))

        println("Test thread " + Thread.currentThread().getName)

        val f: Future[List[Int]] = Future.sequence(fs)
        f onComplete { case x => {
            println("Complete thread " + Thread.currentThread().getName)
            assert(x.get == List(5))
        }}
        Await.ready(f, Duration.Inf)
   }
}

When run we get this output and the test doesn't fail:

Test thread: ScalaTest-run-running-MyTest
Complete thread: ForkJoinPool-1-worker-11

You want your assertion to be in the same thread as your test. Ultimately I think you're looking for something like this outside of your onComplete:

assert(Await.result(f, 5 seconds) == List(1, 2, 3))

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)