在同一表格中加入类别和子类别

时间:2018-09-03 16:57:55

标签: php mysql sql

我有下表-

类别

+-----------+-------------+
|  column   |    type     |
+-----------+-------------+
| id        | int(11)     |
| ctid      | int(11)     |
| name      | varchar(40) |
| ToAmount  | int(11)     |
| is_active | tinyint(1)  |
+-----------+-------------+

子类别

+-----------+-------------+
|  column   |    type     |
+-----------+-------------+
| id        | int(11)     |
| sid       | varchar(40) |
| name      | varchar(50) |
| Amount    | int(11)     |
+-----------+-------------+

我需要连接这两个表,结果表应该在一个字段中包含类别和子类别

示例表一具有类别,其他项也具有

类别

+----+------------+--------------+------------+
| id |  name      | ctid         |ToAmount    |
+----+------------+--------------+------------+
|  1 | apple      |              | 100        |
|  2 | nonveg     |     1        | 350        |
|  3 | orange     |              | 150        |
|  4 | furniture  |     2        | 200        |
+----+------------+--------------+------------+

子类别

+----+--------+--------------+------------+
| id |  name  |   sid        |Amount      |
+----+--------+--------------+------------+
|  1 | chair  |     2        | 100        |
|  2 | meat   |     1        | 200        |
|  3 | chicken|     1        | 150        |
|  4 | stool  |     2        | 100        |
+----+--------+--------------+------------+

我需要加入这两个表并在一个字段中进行 预期结果

+----+--------+--------------+
| id |  name  |   Amount     |
+----+--------+--------------+
|  1 | apple  |   100        |
|  2 | meat   |   200        |
|  3 | chicken|   150        |
|  4 | orange |   150        |
|  5 | stool  |   100        |         
|  6 | chair  |   100        |
+----+--------+--------------+

我得到的结果

SELECT `categories`.`id`,
           `categories`.`ToAmount`AS Ccost,
           `ategories`.`name ` AS Sname,
           `subcategories`.`name` As Noname,
           `subcategories`.`Amount` As Scost
    FROM (`categories`)
    LEFT JOIN `subcategories` 
        ON `categories`.`ctid` = `subcategories`.`sid`

+----+----------+--------------+-------------+----------+
| id |  Cname   | Ccost        |   Sname     | Scost    |
+----+----------+--------------+-------------+----------+
|  1 | apple    | 100          |             |          |
|  2 | nonveg   | 350          | meat        | 200      |
|  3 | nonveg   | 350          | chicken     | 150      |
|  4 | furnitere| 200          | stool       | 100      |
|  5 | furnitere| 200          | chair       | 100      |
|  6 | orange   | 150          |             |          |
+----+----------+--------------+-------------+----------+

在此结果中,列出家具和非蔬菜类别,我不希望此类别在结果表中列出

3 个答案:

答案 0 :(得分:0)

这应该给您预期的结果(根据问题的模糊条件,有些虚假的解决方案):

SET @rownum := 0;
SELECT @rownum:=@rownum+1 AS id,
       name,
       Amount
FROM (SELECT
        Sub.`name` AS name,
        Sub.`Amount` AS Amount
      FROM `subcategories` as Sub
      UNION
      SELECT
        Cat.`name` AS name,
        Cat.`ToAmount` AS Amount
      FROM `categories` as Cat
      WHERE Cat.`ctid` IS NULL) AS Commontable

请注意,您的类别还包含子类别(例如,苹果和橙子),因此,如果我没有记错的话,表的结构最初是错误的。

答案 1 :(得分:0)

我认为您正在寻找coalesce()

SELECT c.`id`,
       COALESCE(sc.Amount, c.ToAmount) as cost,
       COALESCE(sc.name, c.name) as Name
FROM categories c LEFT JOIN
     subcategories sc
     ON c.ctid = sc.sid;

这个想法是做一个LEFT JOIN。如果该值存在于subcategories中,请使用它。否则,请使用categories中的值。

答案 2 :(得分:0)

select C.id ,
  case when s.name is null then c.name else s.name  end as name ,
  case when s.amount is null then c.ToAmount else s.amount  end as amount
   from Categories  c
left  join  Subcategories s on s.sid=c.ctid