douqiao1413 2019-07-03 13:40
浏览 125

如何摆脱右连接中的空值

I have Three Table

categories

id
name

Sub_Categories

id
name
category_id

Sub_categories_three

id
name
sub_categories

Now I want to show all the data (name) from these table in a dropdown list, what would be the query to do so and how to set them on a view (nested loop) using foreach or mysqli_fetch_assoc().

I am using this query

$query="SELECT a.c_name, a.id, b.sc_name, b.id, c.sct_name, c.id 
FROM categories a 
right JOIN sub_categories b ON a.id=b.id 
right JOIN sub_categories_three c ON b.id=c.id";

But this query is returning null values also that fill unnecessary space in my dropdown tag.

Here is the image showing the null values that I don't want:

image

  • 写回答

1条回答 默认 最新

  • dtn36013 2019-07-03 14:25
    关注

    So frist of all you do not need 3 tables. You can club them all together! This leads to following schema:

    create table category
    (
        id       int primary key,
        name     varchar(255) not null,
        superior int
    );
    

    So far, so good. Now we need some dummy data. For that execute following statement:

    insert into category (id, name, superior)
    values (1, 'Category 1', null),
           (2, 'Category 2', 1),
           (3, 'Category 3', 1),
           (4, 'Category 4', 1),
           (5, 'Category 5', 1),
           (6, 'Category 6', 2),
           (7, 'Category 7', 2),
           (8, 'Category 8', 2),
           (9, 'Category 9', 2),
           (10, 'Category 10', 3),
           (11, 'Category 11', 3),
           (12, 'Category 12', 3),
           (13, 'Category 13', 3),
           (14, 'Category 14', 4),
           (15, 'Category 15', 4),
           (16, 'Category 16', 7),
           (17, 'Category 17', 7),
           (18, 'Category 18', 8),
           (19, 'Category 19', 8),
           (20, 'Category 20', 8);
    

    Now all you need to do is a recursive SQL statement. In this case I want Category 7 and all its subcategories:

    with recursive subcategory as (
        select id, name, superior
        from category
        where id = 7
        union
        select c.id, c.name, c.superior
        from category c
                 inner join subcategory s on s.id = c.superior
    )
    select *
    from subcategory;
    

    The result is (CSV-Format):

    id, | name,        | superior
    ---------------------------
    7,  | Category 7,  | 2
    16, | Category 16, | 7
    17, | Category 17, | 7
    

    This is how I suppose to solve your problem.

    This article might help you as well.

    Cheers and stay tuned!

    silenum

    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序