我正在尝试加入MySQL中的几个表。我们的设置有点独特,所以我尽力解释。
示例:
一个组件就像一个部件,让我们说一个螺丝。该螺钉用于计算机中。可以在多台计算机上使用相同的螺钉。但每台计算机只能用于一个特定的安装。
TABLE COMPOMENT_PRODUCT
COMPOMENT_ID PRODUCT_ID
1 1
1 2
2 1
2 2
因此,我们将组件C1和C2与两个安装相关联。
TABLE INVENTORY
COMPOMENT_ID INSTALLATION_ID ON_STOCK
1 1 5
1 2 2
我想要实现的目标 现在,我想检索所有组件的库存状态。但是,并非每个组件都有库存记录。在这些情况下,库存中的ON_STOCK值应为NULL
这意味着,对于这个例子,我期望得到以下结果
COMPOMENT_ID PRODUCT_ID ON_STOCK
1 1 5
1 2 2
2 1 NULL
2 2 NULL
但执行此查询:
SELECT DISTINCT
COMPONENT_PRODUCT.COMPONENT_ID,
COMPONENT_PRODUCT.PRODUCT_ID,
INVENTORY.ON_STOCK
FROM INVENTORY
RIGHT JOIN COMPONENT_PRODUCT ON COMPONENT_PRODUCT.COMPONENT_ID =
INVENTORY.COMPONENT_ID
返回以下结果集:
COMPONENT_ID PRODUCT_ID ON_STOCK
1 1 5
1 2 5
1 1 2
1 2 2
2 1 (null)
2 2 (null)
现在,我的下一个想法是,“当然,这是联接的行为,好吧我需要对结果进行分组”。但是SQL的工作方式,聚合并不是完全可以预测的。所以,当我
GROUP BY COMPONENT_PRODUCT.COMPONENT_ID,COMPONENT_PRODUCT.PRODUCT_ID
我得到了这个结果:
COMPONENT_ID PRODUCT_ID ON_STOCK
1 1 5
1 2 5
2 1 (null)
2 2 (null)
我在这里准备了一个小提琴:http://sqlfiddle.com/#!9/71ca87
我忘记了什么?提前感谢任何指针。
答案 0 :(得分:0)
尝试此查询 -
module.exports = function (grunt) {
'use strict';
grunt.loadNpmTasks('grunt-shell');
/**
* This callback is passed via the `go_to_folder` Task.
* If the folder named `quux` is missing it is reported to the Terminal
* and then invokes the `create_folder` Task.
* However, if the folder named `quux` does exists it reports successfully
* changing to the directory.
*/
function goToFolderCB(err, stdout, stderr, cb) {
// Check if the OS error reports the folder is missing:
// - `No such file or directory` is reported in MacOS (Darwin)
// - `The system cannot find the path specified` is reported in Windows cmd.exe
if (stderr && stderr.indexOf('No such file or directory') > -1
|| stderr.indexOf('The system cannot find the path specified') > -1) {
grunt.log.writeln('The folder named \'quux\' is missing');
grunt.task.run('shell:create_folder');
} else {
grunt.log.ok('Done: Successfully changed to directory \'quux\'');
}
cb();
}
/**
* This callback is passed via the `create_folder` task.
* After reporting successfully creating the folder named `quux`
* it runs the `go_to_folder` Task.
*/
function createFolderCB(err, stdout, stderr, cb) {
grunt.log.writeln('Successfully created folder named \'quux\'');
grunt.task.run('shell:go_to_folder');
cb();
}
grunt.initConfig({
shell: {
go_to_folder: {
command: 'cd quux',
options: {
stderr: false, // Prevent stderr logs in Terminal.
callback: goToFolderCB
}
},
create_folder: {
command: 'mkdir quux',
options: {
callback: createFolderCB
}
}
}
});
grunt.task.registerTask('test', function () {
grunt.log.header('Running app');
grunt.task.run('shell:go_to_folder');
});
grunt.registerTask('default', ['test']);
};