Oracle - 尝试从同一查询中的两个表中查询数据

时间:2015-08-27 13:37:31

标签: mysql oracle

我试图主要从一个表中查询数据,但我还需要来自同一数据库中另一个表的两列。我的查询的一部分看起来像这样:

SELECT
      'Contact 1' AS "Phone 2 - Type",
      Emerg_Phone_1 AS "Phone 2 - Value",
      'Contact 2' AS "Phone 3 - Type",
      Emerg_Phone_2 AS "Phone 3 - Value",
      'Student Cell' AS "Phone 4 - Type",
      STUDENTCELL AS "Phone 4 - Value",
FROM STUDENTS JOIN STUDENTS2

所以基本上,除了STUDENTCELL以外,所有五个选择都来自STUDENTS表,但我在同一个查询中需要STUDENTS2表中的一列STUDENTCELL。感谢您提供任何帮助和建议,我对mySQL和oracle都很陌生。

2 个答案:

答案 0 :(得分:1)

你几乎得到了解决方案。除了join子句之外。您需要使用common列连接两个表。因此,您可以使用ON ClauseUsing clause。 b / w的基本差异是

USING子句

  

如果多个列共享相同的名称但您不想使用所有这些公共列进行连接,则使用USING子句。 USING子句中列出的列在语句中不能包含任何限定符,包括WHERE子句:

ON条款

  

ON子句用于连接两个表中列名不匹配的表。连接条件将从WHERE子句中的过滤条件中删除:

我将Common列视为ID。如果两个表都有不同的列名,请使用on

SELECT
      'Contact 1' AS "Phone 2 - Type",
      Emerg_Phone_1 AS "Phone 2 - Value",
      'Contact 2' AS "Phone 3 - Type",
      Emerg_Phone_2 AS "Phone 3 - Value",
      'Student Cell' AS "Phone 4 - Type",
      STUDENTCELL AS "Phone 4 - Value",
FROM STUDENTS JOIN STUDENTS2 ON (STUDENTS.Stud_ID=STUDENTS2.Stud2_ID)

如果两个表具有相同的列名,则使用using

SELECT
      'Contact 1' AS "Phone 2 - Type",
      Emerg_Phone_1 AS "Phone 2 - Value",
      'Contact 2' AS "Phone 3 - Type",
      Emerg_Phone_2 AS "Phone 3 - Value",
      'Student Cell' AS "Phone 4 - Type",
      STUDENTCELL AS "Phone 4 - Value",
FROM STUDENTS JOIN STUDENTS2 using (id);

您可能需要浏览Oracle JOINSSQL JOINS

答案 1 :(得分:0)

假设var bx1 = $(".bx1"); var bx2 = $(document.querySelectorAll(".bx2")); 是两个表中的公共字段,以下查询应该执行作业

student_id