Skip to content

Conversation

@dingyuanhong
Copy link

example:
CREATE TABLE IF NOT EXISTS app
(
id INT NOT NULL AUTO_INCREMENT,
parent_app_id INT NOT NULL DEFAULT 0 COMMENT '合并父应用id',
PRIMARY KEY (id) USING BTREE
) AUTO_INCREMENT = 105 COMMENT = 'API所属的应用信息';

CREATE TABLE IF NOT EXISTS user_account (
id BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键id',
name VARCHAR(255) COMMENT '账号名称',
PRIMARY KEY (id) USING BTREE
) AUTO_INCREMENT = 1 COMMENT='账号表,存储账号的基本信息和安全评分';

-- name: GetUserAccountAllChildIDs :exec
WITH app_user_account_id AS (
SELECT tua.id,
tua.name,
CAST(IFNULL(tb.id, ta.id) AS SIGNED) AS parent_app_id
FROM user_account AS tua
LEFT JOIN app AS ta ON ta.id = tua.app_id
LEFT JOIN app as tb ON ta.parent_app_id = tb.id
WHERE tua.masked_sign = 1 AND tua.deleted_sign = 1
AND ta.masked_sign = 1
AND ta.deleted_sign = 1
)
SELECT a.name
FROM app_user_account_id AS a
LEFT JOIN app_user_account_id AS b ON b.id IN (sqlc.slice('account_id_set')) AND a.name = b.name AND a.parent_app_id = b.parent_app_id;

example:
CREATE TABLE IF NOT EXISTS `app`
(
    `id`                        INT          NOT NULL AUTO_INCREMENT,
    `parent_app_id`             INT          NOT NULL DEFAULT 0 COMMENT '合并父应用id',
    PRIMARY KEY (`id`) USING BTREE
) AUTO_INCREMENT = 105 COMMENT = 'API所属的应用信息';

CREATE TABLE IF NOT EXISTS `user_account` (
    `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键id',
    `name` VARCHAR(255) COMMENT '账号名称',
    PRIMARY KEY (`id`) USING BTREE
) AUTO_INCREMENT = 1 COMMENT='账号表,存储账号的基本信息和安全评分';

-- name: GetUserAccountAllChildIDs :exec
WITH app_user_account_id AS (
    SELECT tua.id,
           tua.name,
           CAST(IFNULL(tb.id, ta.id) AS SIGNED) AS parent_app_id
    FROM user_account AS tua
             LEFT JOIN app AS ta ON ta.id = tua.app_id
             LEFT JOIN app as tb ON ta.parent_app_id = tb.id
    WHERE tua.masked_sign = 1 AND tua.deleted_sign = 1
      AND ta.masked_sign = 1
      AND ta.deleted_sign = 1
)
SELECT a.name
FROM app_user_account_id AS a
LEFT JOIN app_user_account_id AS b ON b.id IN (sqlc.slice('account_id_set')) AND a.name = b.name AND a.parent_app_id = b.parent_app_id;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant