这个SQL查询有什么问题

时间:2019-06-04 06:36:41

标签: mysql sql

我创建了两个表“ tbl_book_info”和“ tbl_books”(表说明 在下面)在MySQL数据库中

我在SQL查询中应该更改什么

我在WHERE子句中使用了子查询。

“ tbl_books”的所有记录-

+-------+-------+--------+
| accid | accno | status |
+-------+-------+--------+
| 10001 |   101 | I      |
| 10001 |   102 | I      |
| 10001 |   103 | A      |
| 10002 |   101 | A      |
| 10002 |   102 | A      |
| 10002 |   103 | I      |
| 10002 |   104 | I      |
| 10002 |   105 | I      |
| 10003 |   101 | A      |
| 10003 |   102 | A      |
| 10003 |   103 | A      |
| 10003 |   104 | I      |
| 10003 |   105 | I      |
| 10004 |   101 | A      |
| 10004 |   102 | I      |
| 10004 |   103 | A      |
| 10004 |   104 | A      |
| 10004 |   105 | A      |
| 10005 |   101 | A      |
| 10005 |   102 | A      |
| 10005 |   103 | A      |
| 10005 |   104 | A      |
| 10005 |   105 | A      |
+-------+-------+--------+
23 rows in set (0.00 sec)

“ tbl_book_info”的所有记录-

+----------+------------------+-------+-------------+---------+---------+-------------+---------+--------+---------+---------+
| b_acc_id | b_name           | b_qty | b_type      | b_auth1 | b_auth2 | b_pub       | b_pages | b_rack | b_price | b_about |
+----------+------------------+-------+-------------+---------+---------+-------------+---------+--------+---------+---------+
|    10001 | Java             |     3 | Programming | lala    | -       | kallo       |     800 |      1 |  799.00 | -       |
|    10002 | Cpp              |     5 | Programming | Kallo   | -       | Mehta group |     400 |      2 |  300.00 | -       |
|    10003 | VB.net           |     5 | Programming | lalaji  | -       | amam co.    |     479 |      3 |  100.00 | -       |
|    10004 | DBMS             |     5 | prog        | lalal   | -       | kallo       |     888 |      3 |  499.00 | -       |
|    10005 | computer network |     5 | Networking  | Mirabai | -       | kabirdas    |     789 |      2 |  800.00 | -       |
+----------+------------------+-------+-------------+---------+---------+-------------+---------+--------+---------+---------+

desc tbl_books;

+--------+------------+------+-----+---------+-------+ | Field  | Type       | Null | Key | Default | Extra |
+--------+------------+------+-----+---------+-------+ | accid  | int(5)     | NO   | PRI | 0       |       | | accno  | int(3)     | NO | PRI | 0       |       | | status | varchar(1) | YES  |     | A       |       |
+--------+------------+------+-----+---------+-------+

desc tbl_book_info;

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| b_acc_id | int(5)       | NO   | PRI | NULL    | auto_increment |
| b_name   | varchar(50)  | NO   |     | NULL    |                |
| b_qty    | int(2)       | NO   |     | NULL    |                |
| b_type   | varchar(30)  | NO   |     | NULL    |                |
| b_auth1  | varchar(50)  | NO   |     | NULL    |                |
| b_auth2  | varchar(50)  | YES  |     | NULL    |                |
| b_pub    | varchar(50)  | NO   |     | NULL    |                |
| b_pages  | int(4)       | NO   |     | NULL    |                |
| b_rack   | int(5)       | NO   |     | NULL    |                |
| b_price  | decimal(6,2) | NO   |     | NULL    |                |
| b_about  | text         | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+

此处tbl_books.accid参考tbl_book_info.b_acc_id

我的查询是:

select b_name, b_qty, b_acc_id , (count(*)) Issued, (count(*)) Available
From tbl_book_info
where tbl_book_info.b_acc_id in (select accid from tbl_books
                                 where status = 'I'
                                 GROUP BY status) ;

我想在表'tbl_book_info'和'tbl_books'之间执行联接以将结果打印为

所需的输出:

+--------+-------+----------+--------+-----------+
| b_name | b_qty | b_acc_id | Issued | Available |
+--------+-------+----------+--------+-----------+
| Java   |     3 |    10001 |      2 |         1 |
+--------+-------+----------+--------+-----------+
| Cpp    |     5 |    10002 |      3 |         2 |
+--------+-------+----------+--------+-----------+

...(还有更多)

输出来了:

+--------+-------+----------+--------+-----------+
| b_name | b_qty | b_acc_id | Issued | Available |
+--------+-------+----------+--------+-----------+
| Java   |     3 |    10001 |      4 |         4 |
+--------+-------+----------+--------+-----------+

3 个答案:

答案 0 :(得分:1)

如果要执行Join,则应该这样做,而不要在where中进行奇怪的子查询。您可以进行Join和有条件的案例,以在两个不同的列中汇总Issued和Avalilables

Select a.b_name, a.b_qty, a.b_acc_id , sum(case 
                                        when b.status='I' then 1
                                        else 0
                                     end) as Issued,
                                     sum(case 
                                        when b.status='A' then 1
                                        else 0
                                     end) as Available
From tbl_book_info a left join tbl_books b on a.b_acc_id=b.accid
group by a.b_name, a.b_qty, a.b_acc_id

答案 1 :(得分:0)

在MySQL中,您可以为此类查询捷径:

select bi.b_name, bi.b_qty, bi.b_acc_id, 
       sum( b.status = 'I' ) as Issued,
       sum( b.status = 'A' ) as Available
from tbl_book_info bi left join
     tbl_books b 
     on bi.b_acc_id = b.accid
group by bi.b_name, bi.b_qty, bi.b_acc_id;

MySQL将布尔表达式视为数字上下文中的整数(例如SUM())。另外请注意,表别名是表名称的缩写。

使用books(acc_id, status)上的索引,使用子查询可能会更快:

select bi.b_name, bi.b_qty, bi.b_acc_id, 
       (select count(*)
        from tbl_books b
        where b.accid = bi.b_acc_id and
              b.status = 'I'
       ) as Issued,
       (select count(*)
        from tbl_books b
        where b.accid = bi.b_acc_id and
              b.status = 'A'
       ) as Available
from tbl_book_info bi ;

通过避免使用外部group by(通常是一种排序)来提高性能。可以改用索引。

答案 2 :(得分:-2)

我还没有尝试过此解决方案,但是可以说您需要对accid表的tbl_books列进行分组。因此,请在查询中进行以下更改:

select b_name, b_qty, b_acc_id , (count(*)) Issued, (count(*)) Available
From tbl_book_info
where tbl_book_info.b_acc_id in (select accid from tbl_books
                                 where status = 'I'
                                 GROUP BY accid) ;