请问如何将这多个SELECT语句的结果合并到一个结果集中?

时间:2017-05-17 00:03:06

标签: mysql node.js express

以下是架构和各个select语句。 但是,我想检索各种查询的所有结果。 我是

// SCHEMA ..这是db模式

CREATE TABLE `orgs` (

  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `org_name` varchar(250) NOT NULL,
  UNIQUE (org_name)
)



CREATE TABLE `orgs_relation` (

  `org_id` int(10) UNSIGNED NOT NULL,
  `parent_org_id` int(10) UNSIGNED NOT NULL,
  CONSTRAINT org_relation_pair UNIQUE (org_id, parent_org_id)
)


#// Get parents
SELECT parent_org_id, orgs.org_name as org_name, "parent" as parent FROM `orgs_relation`
JOIN orgs ON orgs_relation.parent_org_id = orgs.id
WHERE org_id = (SELECT id FROM orgs WHERE org_name='Black Banana')
ORDER BY org_name ASC

#// Get chidren
SELECT org_id, orgs.org_name as org_name, "children" as children FROM `orgs_relation`
JOIN orgs ON orgs_relation.org_id = orgs.id
WHERE parent_org_id = (SELECT id FROM orgs WHERE org_name='Black Banana')
ORDER BY org_name ASC

#// Get sisters
SELECT DISTINCT or2.org_id AS sister_id, org_name, "sisters" as sisters FROM `orgs_relation` AS or1
JOIN orgs_relation AS or2 ON or1.parent_org_id = or2.parent_org_id
JOIN orgs ON or2.org_id = orgs.id
WHERE or1.org_id = (SELECT id FROM orgs WHERE org_name='Black Banana')
ORDER BY org_name ASC

我目前正在使用以下方法,但它无效:

app.get("/api/orgs/all/:id", function(req, res){
    var p = req.params.id;
    console.log(p);

var daughtersQuery = "SELECT org_id, orgs.org_name as org_name, 'daughters' as daughters FROM `orgs_relation` JOIN orgs ON orgs_relation.org_id = orgs.id  WHERE parent_org_id = (SELECT id FROM orgs WHERE org_name =  'Black Banana')";
var parentQuery = "SELECT parent_org_id, orgs.org_name as org_name, 'parent' as parent FROM `orgs_relation`JOIN orgs ON orgs_relation.parent_org_id = orgs.id WHERE org_id = (SELECT id FROM orgs WHERE org_name= 'Black Banana')";   
var sistersQuery = "SELECT DISTINCT or2.org_id AS sister_id, org_name, 'sisters' as sisters FROM `orgs_relation` AS or1 JOIN orgs_relation AS or2 ON or1.parent_org_id = or2.parent_org_id JOIN orgs ON or2.org_id = orgs.id WHERE or1.org_id = (SELECT id FROM orgs WHERE org_name= 'Black Banana')";

   var q = "SELECT DISTINCT id, org_name, org_id, parent_org_id from (" +
                        daughtersQuery + " UNION ALL " +
                        parentQuery + " UNION ALL " +
                        sistersQuery + " ) a ORDER ALL org_name asc";
      //  console.log(q);

         connection.query(q, function (error, results) {
              if (error) throw error;
          // console.log(results);
          res.send(results);
        });
});

1 个答案:

答案 0 :(得分:0)

我假设你正在使用node-mysqldocs州:

  

出于安全原因,禁用对多个语句的支持(它   如果没有正确转义值,则允许SQL注入攻击。)

多个语句查询

要使用此功能,您必须为连接启用它:

var connection = mysql.createConnection({multipleStatements: true});

启用后,您可以使用分号;分隔每个语句,从而执行多个语句的查询。结果将是每个语句的数组。

实施例

connection.query('SELECT ?; SELECT ?', [1, 2], function(err, results) {
  if (err) throw err;

  // `results` is an array with one element for every statement in the query:
  console.log(results[0]); // [{1: 1}]
  console.log(results[1]); // [{2: 2}]
});

你的答案

var q = "SELECT org_id, orgs.org_name as org_name, 'daughters' as daughters FROM `orgs_relation` JOIN orgs ON orgs_relation.org_id = orgs.id  WHERE parent_org_id = (SELECT id FROM orgs WHERE org_name =  'Black Banana');SELECT parent_org_id, orgs.org_name as org_name, 'parent' as parent FROM `orgs_relation`JOIN orgs ON orgs_relation.parent_org_id = orgs.id WHERE org_id = (SELECT id FROM orgs WHERE org_name= 'Black Banana');SELECT DISTINCT or2.org_id AS sister_id, org_name, 'sisters' as sisters FROM `orgs_relation` AS or1 JOIN orgs_relation AS or2 ON or1.parent_org_id = or2.parent_org_id JOIN orgs ON or2.org_id = orgs.id WHERE or1.org_id = (SELECT id FROM orgs WHERE org_name= 'Black Banana')";

connection.query(q, function (error, results) {
  if (error) throw error;
  // console.log(results[0]);
  // console.log(results[1]);
  // console.log(results[2]);
  res.send(results);
});