根据表主键从两个表中选择

时间:2017-06-28 07:38:29

标签: mysql sql

我有一个表格,表示两种类型记录的常用值,我有另外两个表来保存彼此不同的数据。表1如下

银行

+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| officer         | varchar(32)  | NO   |     | NULL    |                |
| bank            | varchar(64)  | NO   |     | NULL    |                |
| branch          | varchar(64)  | NO   |     | NULL    |                |
| amount          | int(11)      | NO   |     | NULL    |                |
| date            | date         | NO   |     | NULL    |                |
| sys_date        | date         | NO   |     | NULL    |                |
| source_document | varchar(360) | NO   |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+

machinery_banking

+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| banking_id     | int(11)     | NO   |     | NULL    |       |
| engine_no      | varchar(64) | NO   |     | NULL    |       |
| chassis_no     | varchar(64) | NO   |     | NULL    |       |
| receipt_number | varchar(64) | NO   |     | NULL    |       |
| payment_type   | int(11)     | NO   |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+

spare_parts_banking

+----------------+-------------+------+-----+---------+-------+
| Field          | Type        | Null | Key | Default | Extra |
+----------------+-------------+------+-----+---------+-------+
| banking_id     | int(11)     | NO   |     | NULL    |       |
| invoice_number | varchar(32) | NO   |     | NULL    |       |
+----------------+-------------+------+-----+---------+-------+

输入到银行业务表的每个数据都有其他数据,每个表中有一个表machine_banking或spare_parts_banking。

我在银行

中有以下数据
+----+---------+------------+------------+-----------+------------+------------+------------------------------------------------------------------------------------------+
| id | officer | bank       | branch     | amount    | date       | sys_date   | source_document                                                                          |
+----+---------+------------+------------+-----------+------------+------------+------------------------------------------------------------------------------------------+
|  1 | prasad  | Sampath    | Kaduruwela | 234234234 | 2017-06-28 | 2017-06-28 | image |
|  2 | prasad  | Commercial | Colombo    | 234234234 | 2017-05-28 | 2017-05-28 | image |
+----+---------+------------+------------+-----------+------------+------------+------------------------------------------------------------------------------------------+

我在 machinery_banking

中有以下数据
+------------+-----------+------------+----------------+--------------+
| banking_id | engine_no | chassis_no | receipt_number | payment_type |
+------------+-----------+------------+----------------+--------------+
|          1 | 2324234   | NL234234   | RAN234         |            1 |
+------------+-----------+------------+----------------+--------------+

spare_parts_banking 中的数据如下

+------------+----------------+
| banking_id | invoice_number |
+------------+----------------+
|          2 | INVRAN1        |
+------------+----------------+

我尝试了以下查询

SELECT a.id, a.officer, a.bank, a.branch, a.amount, a.date, a.sys_date, b.engine_no, b.chassis_no, a.source_document, c.invoice_number
FROM banking a,machinery_banking b, spare_parts_banking c
WHERE   a.id = b.banking_id OR a.id = c.banking_id;

并最终得出以下结果

+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
| id | officer | bank       | branch     | amount    | date       | sys_date   | engine_no | chassis_no | source_document                                                                          | invoice_number |
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
|  1 | prasad  | Sampath    | Kaduruwela | 234234234 | 2017-06-28 | 2017-06-28 | 2324234   | NL234234   | http://res.cloudinary.com/randeepa-com/image/upload/v1498455394/dmhxqal8hjcthhgav0n9.jpg | INVRAN1        |
|  2 | prasad  | Commercial | Colombo    | 234234234 | 2017-05-28 | 2017-05-28 | 2324234   | NL234234   | http://res.cloudinary.com/randeepa-com/image/upload/v1498455394/dmhxqal8hjcthhgav0n9.jpg | INVRAN1        |
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+

我希望获得的结果如下

+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
| id | officer | bank       | branch     | amount    | date       | sys_date   | engine_no | chassis_no | source_document                                                                          | invoice_number |
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+
|  1 | prasad  | Sampath    | Kaduruwela | 234234234 | 2017-06-28 | 2017-06-28 | 2324234   | NL234234   | http://res.cloudinary.com/randeepa-com/image/upload/v1498455394/dmhxqal8hjcthhgav0n9.jpg |                |  
|  2 | prasad  | Commercial | Colombo    | 234234234 | 2017-05-28 | 2017-05-28 |           |            | http://res.cloudinary.com/randeepa-com/image/upload/v1498455394/dmhxqal8hjcthhgav0n9.jpg | INVRAN1        |
+----+---------+------------+------------+-----------+------------+------------+-----------+------------+------------------------------------------------------------------------------------------+----------------+

我可以使用什么查询来获得此结果

2 个答案:

答案 0 :(得分:1)

为什么你有数据重复的原因是因为条件之间的OR

我相信这应该有用

SELECT a.id,
    a.officer,
    a.bank,
    a.branch,
    a.amount,
    a.date,
    a.sys_date,
    b.engine_no,
    b.chassis_no,
    a.source_document,
    c.invoice_number
FROM banking a
LEFT JOIN machinery_banking b
ON  a.id = b.banking_id
LEFT JOIN spare_parts_banking c
ON  a.id = c.banking_id;

同样转向新的JOIN语法,它更容易理解。

SQL连接语法 - Reference

答案 1 :(得分:0)

试试这个:

YOUR_LISTVIEW.setOnScrollListener(new OnScrollListener() {
    private int currentVisibleItemCount;
    private int currentScrollState;
    private int currentFirstVisibleItem;
    private int totalItem;
    private LinearLayout lBelow;


    @Override
    public void onScrollStateChanged (AbsListView view,int scrollState){
        // TODO Auto-generated method stub
        this.currentScrollState = scrollState;
        this.isScrollCompleted();
    }

    @Override
    public void onScroll (AbsListView view,int firstVisibleItem,
    int visibleItemCount, int totalItemCount){
        // TODO Auto-generated method stub
        this.currentFirstVisibleItem = firstVisibleItem;
        this.currentVisibleItemCount = visibleItemCount;
        this.totalItem = totalItemCount;


    }

    private void isScrollCompleted () {
        if (totalItem - currentFirstVisibleItem == currentVisibleItemCount
                && this.currentScrollState == SCROLL_STATE_IDLE) {
            /** To do code here*/
            Dbref.orderByKey().startAt(oldestPostId).limitToFirst(10).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot child : dataSnapshot.getChildren()) {

                        oldestPostId = child.getKey();
                        String event_id = snapshot.child("details").child("event_id").getValue().toString();
                        e = new Event(event_id); //Event is a model class for list items
                        event.add(e);
                        Log.e("ShowEventInfo : ", "" + event_id);
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    });

});