从DB

时间:2016-09-06 10:22:13

标签: mysql sql database

在我的数据库中,我有两个表:

1. "subjects" table: that include 2 fileds:
    *id, *desctiption,
2. "subSubjects" table that include 4 fileds: 
   *id, *Desctiption, *subject_id, *department_id (primary key of departments    table).

我需要返回主题列表,每个主题包含里面匹配SubSubjects的列表, 有一种方法可以做到这一点,而不使用选择内部selecet声明? (加入或其他声明)? *我有一个类 - “主题”,包括subSubjects的Arraylist。

视野中的结果需要像这样的结果:

*****************************
subject * sub-subject * dep *
*****************************
sub_1   * sub sub1    * 1   *
        *********************
        * sub sub2    * 2   *
*****************************
sub_2   * sub sub 4   * 1   *
*****************************
sub_3   * sub sub 3   * 1   *
        *********************
        * sub sub 5   * 2   *
        *********************
        * sub sub 6   * 2   *
*****************************

3 个答案:

答案 0 :(得分:1)

使用以下查询:

SELECT
CASE WHEN t.id = (SELECT TOP 1 id -- Sub query
FROM Subsubjects t3
WHERE t3.subject_id = t.subject_id
ORDER BY t3.id) THEN q.description
ELSE ''
END AS Subjects, t.description AS Subsubjects, t.department_id
FROM Subsubjects t
LEFT JOIN subjects q ON q.id = t.subject_id
ORDER BY t.id

它将返回预期数据如下:

*****************************
  subject * sub-subject * dep *
  *****************************   
  sub_1   * sub sub1    * 1   *
    *********************
          * sub sub2    * 2   *
  *****************************
  sub_2   * sub sub 4   * 1   *
  *****************************
  sub_3   * sub sub 3   * 1   *
    *********************
          * sub sub 5   * 2   *
    *********************
          * sub sub 6   * 2   *
*****************************

以下是我们如何使用sql查询处理列表对象的示例:

public List<Product> GetAllProducts() //GetAllProducts() is a list-type method
{
    Query = "SELECT * FROM Products";

    Command = new SqlCommand(Query, Connection);

    Connection.Open();

    Reader = Command.ExecuteReader();

    List<Product> products = new List<Product>(); //Created a list

    while (Reader.Read())
    {
        Product product = new Product(); //Created an object from the class
        product.ProductId = Convert.ToInt32(Reader["ProductID"]);
        product.CategoryId = Convert.ToInt32(Reader["CategoryID"]);
        product.ProductName = Reader["ProductName"].ToString();
        product.Details = Reader["Details"].ToString();
        product.Price = (double)Reader["Price"];
        product.Stock = Convert.ToDouble(Reader["Stock"]);

        products.Add(product); //Finally bind the object with the list
    }

    Reader.Close();
    Connection.Close();

    return products;
}

答案 1 :(得分:0)

SELECT s.id, ss.description, ss.department_id FROM `subjects` AS s INNER JOIN `subSubjects` AS ss ON s.id=ss.subject_id

查看W3 Join,这将解释您需要了解的有关创建内部联接和其他各种SQL语句的基础知识。

答案 2 :(得分:0)

如果所有子主题始终与主题相关,则应尝试内连接, 否则尝试左外连接

SELECT   as Subject, 
       subSubjects.Description as SubSubject, 
       department_id as Dep 
FROM subSubjects
INNER JOIN Subjects
ON Subjects.id=SubSubjects.subject_id
ORDER BY Subjects.description