Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MySQL-连接多个select语句_Mysql_Multiple Select_Multiple Select Query - Fatal编程技术网

MySQL-连接多个select语句

MySQL-连接多个select语句,mysql,multiple-select,multiple-select-query,Mysql,Multiple Select,Multiple Select Query,我最初尝试使用占用太多资源的单个查询获取数据,因此我尝试合并这些查询以减少所需时间。第一个选择本身工作良好,但我想与第二个选择结合使用,以减少所需时间 当前错误:#1241-操作数应包含1列 希望有以下结果 +--------------+----------+-------------+---------+-------------+------------+-------------+----------------+ | Description | Maincode | Controlc

我最初尝试使用占用太多资源的单个查询获取数据,因此我尝试合并这些查询以减少所需时间。第一个选择本身工作良好,但我想与第二个选择结合使用,以减少所需时间

当前错误:#1241-操作数应包含1列

希望有以下结果

+--------------+----------+-------------+---------+-------------+------------+-------------+----------------+
| Description  | Maincode | Controlcode | Subcode | Mainbalance | Totaldebit | Totalcredit | Openingbalance |
+--------------+----------+-------------+---------+-------------+------------+-------------+----------------+
| Test Account |       01 |          01 |     123 |         100 |        100 |           0 |            200 |
| Test Account |       01 |          02 |     124 |         100 |          0 |         100 |              0 |
+--------------+----------+-------------+---------+-------------+------------+-------------+----------------+

简单的内部连接和一个简单的数学应该可以做到这一点

|子字符串(voucherdate,1,7)| | :--------------------------- | | 2019-07 | | 2019-07 |
dbfiddle

Hey@nbk感谢您的友好回复。但是你没有在SELECT语句中加入WHERE部分。我为WHERE添加了一些临界值,但是你的工作我只展示了如何达到你想要的结果。
 Subcode Table
    +----------+-------------+---------+--------------+
    | Maincode | Controlcode | Subcode | Description  |
    +----------+-------------+---------+--------------+
    |       01 |          01 |     123 | Test Account |
    |       01 |          02 |     124 | Test Account |
    +----------+-------------+---------+--------------+

    Voucher Table
    +-------------+--------------+---------------+----------+-------------+---------+----------------+-------+--------+
    | Voucherdate | Vouchertype  | Vouchernumber | Maincode | Controlcode | Subcode |  Description   | Debit | Credit |
    +-------------+--------------+---------------+----------+-------------+---------+----------------+-------+--------+
    | 2019-07-13  | BV           |            01 |       01 |          01 |     123 | Entering Test  |   100 |      0 |
    | 2019-07-13  | BV           |            01 |       01 |          02 |     124 | Enterting Test |     0 |    100 |
    +-------------+--------------+---------------+----------+-------------+---------+----------------+-------+--------+

    OpeningBalance Table
    +----------+-------------+---------+--------------+---------------+
    | Maincode | Controlcode | Subcode | Debitbalance | Creditbalance |
    +----------+-------------+---------+--------------+---------------+
    |       01 |          01 |     123 |          100 |             0 |
    |       01 |          02 |     124 |          100 |             0 |
    +----------+-------------+---------+--------------+---------------+
+--------------+----------+-------------+---------+-------------+------------+-------------+----------------+
| Description  | Maincode | Controlcode | Subcode | Mainbalance | Totaldebit | Totalcredit | Openingbalance |
+--------------+----------+-------------+---------+-------------+------------+-------------+----------------+
| Test Account |       01 |          01 |     123 |         100 |        100 |           0 |            200 |
| Test Account |       01 |          02 |     124 |         100 |          0 |         100 |              0 |
+--------------+----------+-------------+---------+-------------+------------+-------------+----------------+
CREATE TABLE Subcode
    (`Maincode` int, `Controlcode` int, `Subcode` int, `Description` varchar(12))
;
    
INSERT INTO Subcode
    (`Maincode`, `Controlcode`, `Subcode`, `Description`)
VALUES
    (01, 01, 123, 'Test Account'),
    (01, 02, 124, 'Test Account')
;
✓ ✓
CREATE TABLE Voucher
    (`Voucherdate` Date, `Vouchertype` varchar(2), `Vouchernumber` int, `Maincode` int, `Controlcode` int, `Subcode` int, `Description` varchar(14), `Debit` int, `Credit` int)
;
    
INSERT INTO Voucher
    (`Voucherdate`, `Vouchertype`, `Vouchernumber`, `Maincode`, `Controlcode`, `Subcode`, `Description`, `Debit`, `Credit`)
VALUES
    ('2019-07-13', 'BV', 01, 01, 01, 123, 'Entering Test', 100, 0),
    ('2019-07-13', 'BV', 01, 01, 02, 124, 'Enterting Test', 0, 100)
;
✓ ✓
SELECT * FROM Voucher;
Voucherdate | Vouchertype | Vouchernumber | Maincode | Controlcode | Subcode | Description | Debit | Credit :---------- | :---------- | ------------: | -------: | ----------: | ------: | :------------- | ----: | -----: 2019-07-13 | BV | 1 | 1 | 1 | 123 | Entering Test | 100 | 0 2019-07-13 | BV | 1 | 1 | 2 | 124 | Enterting Test | 0 | 100
CREATE TABLE OpeningBalance
    (`Maincode` int, `Controlcode` int, `Subcode` int, `Debitbalance` int, `Creditbalance` int)
;
    
INSERT INTO OpeningBalance
    (`Maincode`, `Controlcode`, `Subcode`, `Debitbalance`, `Creditbalance`)
VALUES
    (01, 01, 123, 100, 0),
    (01, 02, 124, 100, 0)
;
✓ ✓
SELECT 
s.Description
  ,o.Maincode
  ,o.Controlcode
  ,o.Subcode
  , o.`Debitbalance` Mainbalance
 , v.`Debit` + o.`Debitbalance` Totaldebit 
 , v.`Credit` + o.`Creditbalance` Totalcredit 
 , v.`Debit` + o.`Debitbalance` -( v.`Credit` + o.`Creditbalance`)  Openingbalance 
FROM OpeningBalance o inner Join Voucher v 
ON o.Maincode = v.Maincode AND o.Controlcode = v.Controlcode AND o.Subcode = v.Subcode
INNER JOIN Subcode s ON o.Maincode = s.Maincode AND o.Controlcode = s.Controlcode AND o.Subcode = s.Subcode
WHERE  MONTH(v.voucherdate) <= 7 AND YEAR(v.voucherdate) <= 2019
Description | Maincode | Controlcode | Subcode | Mainbalance | Totaldebit | Totalcredit | Openingbalance :----------- | -------: | ----------: | ------: | ----------: | ---------: | ----------: | -------------: Test Account | 1 | 1 | 123 | 100 | 200 | 0 | 200 Test Account | 1 | 2 | 124 | 100 | 100 | 100 | 0
SELECT (SELECT sc.description AS description, 
           sc.maincode, 
           sc.controlcode, 
           sc.subcode, 
           ( ob.debitbalance - ob.creditbalance ) AS mainbalance 
    FROM   Subcode AS sc 
           LEFT JOIN OpeningBalance AS ob 
                  ON ( sc.maincode = ob.maincode 
                       AND sc.controlcode = ob.controlcode 
                       AND sc.subcode = ob.subcode ) 
    GROUP  BY sc.maincode, 
              sc.controlcode, 
              sc.subcode 
    ORDER  BY sc.maincode, 
              sc.controlcode, 
              sc.subcode ASC) AS test, 
   (SELECT Sum(v.debit)                               AS totaldebit, 
           Sum(v.credit)                              AS totalcredit, 
           ( mainbalance + totaldebit - totalcredit ) AS openingbalance 
    FROM   Subcode AS sc 
           LEFT JOIN Voucher AS v 
                  ON ( sc.maincode = v.maincode 
                       AND sc.controlcode = v.controlcode 
                       AND sc.subcode = v.subcode ) 
    WHERE  Substring(v.voucherdate, 1, 7) < '07-2019' 
    GROUP  BY sc.maincode, 
              sc.controlcode, 
              sc.subcode 
    ORDER  BY sc.maincode, 
              sc.controlcode, 
              sc.subcode ASC) AS test2 
Operand should contain 1 column(s)
SELECT Substring(voucherdate, 1, 7) FROM Voucher
| Substring(voucherdate, 1, 7) | | :--------------------------- | | 2019-07 | | 2019-07 |