Mysql 如何编写select语句来测试一对多表中的两列

Mysql 如何编写select语句来测试一对多表中的两列,mysql,sql,Mysql,Sql,我有两个一对多关系的mysql表 Table a (id, title) Table b (id, a_id, str1, str2) 我想从表A中选择所有行,并包括一个附加列,以指示对于每个子行,如果str1不为null,str2是否也不为null 例如,给定这些数据 Table a (id, title) 1, row1 2, row2 3, row3 4, row4 Table 2 (id, a_id, str1, str2) 1, 2, testa, testb 2, 2, test

我有两个一对多关系的mysql表

Table a (id, title)
Table b (id, a_id, str1, str2)
我想从表A中选择所有行,并包括一个附加列,以指示对于每个子行,如果str1不为null,str2是否也不为null

例如,给定这些数据

Table a (id, title)
1, row1
2, row2
3, row3
4, row4

Table 2 (id, a_id, str1, str2)
1, 2, testa, testb
2, 2, testc, NULL
3, 3, testd, teste
4, 4, NULL, NULL
…我想选择一个如下所示的结果集:

id, title, str1AndStr2
1, row1, 1
2, row2, 0
3, row3, 1
4, row4, 1

这样做的最佳方式是什么?

这可以通过使用MySQL实现。为此,我在
select
子句中插入了case语句,以有条件地显示一个值

select
  a.id
  , a.title
  , case 
    when b.str1 is not null and b.str2 is not null
      then 1
    else 0
  end as str1AndStr2
from
  tableA a
  join tableB b

这可以通过MySQL实现。为此,我在
select
子句中插入了case语句,以有条件地显示一个值

select
  a.id
  , a.title
  , case 
    when b.str1 is not null and b.str2 is not null
      then 1
    else 0
  end as str1AndStr2
from
  tableA a
  join tableB b