pan_cras
2016-11-22 02:37
采纳率: 100%
浏览 2.3k

sql 联表查询(两张表)

SELECT
    teacher_info.teacher_id,
    tearcher_name,
    teacher_number,
    tearcher_type,
    teacher_image,
    teacher_gender,
    teacher_birthday,
    identity_number,
    company,
    technical_title,
    technical_grade,
    major_type_id,
    major_id,
    courses_offer,
    phone,
    email,
    relative_material,
    LEVEL,
    country,
    province,
    city,
    region
FROM
    teacher_info,
    teacher_region_info
WHERE
    teacher_info.teacher_id = teacher_region_info.teacher_id

上面的语句会让teacher_region_info表中如果没有对应的teacher_id就使teacher_info的数据也不显示出来了,如何更改代码?

找到了答案:

SELECT
    teacher_info.teacher_id,
    tearcher_name,
    teacher_region_info.city
FROM
    teacher_info,
    teacher_region_info
WHERE
    teacher_info.teacher_id = teacher_region_info.teacher_id
UNION
    SELECT
        teacher_info.teacher_id,
        tearcher_name,
        teacher_region_info.city
    FROM
        teacher_info,
        teacher_region_info
    WHERE
        teacher_info.teacher_id != NULL
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

7条回答 默认 最新

相关推荐 更多相似问题