简体   繁体   English

mysql选择带有联接的查询

[英]mysql select query with joins

I need some data from database but i am fail to perform query. 我需要数据库中的一些数据,但是我无法执行查询。 i am explaing the data base table and then my desired result can anyone help me to perform the right query. 我正在解释数据库表,然后我想要的结果可以帮助任何人帮助我执行正确的查询。 I shall b very thankful. 我将非常感激。

Table - Bill Status 表格-帐单状态

bill no    saleman    shop           amount
-------------------------------------------
1          umer       Best Mart      3000
2          umer       E-mart         4000
3          umer       Shopping Club  2000
4          umer       The Store      1000
5          umer       Rachna S/S     1500

Table - Ledger 表-分类帐

bill no    saleman    shop          credit    debit
---------------------------------------------------    
1           umer    Best Mart                  200
2           umer    E-mart          100    
            umer    Royal Store     1000    
            umer    Chenab Store    1800    
            umer    Elite Mart      3500    
            umer    The Mart        5000    

Desired Result 所需结果

billno    saleman    shop            amount    credit    debit
--------------------------------------------------------------    
1         umer       Best Mart       3000                 200
2         umer       E-mart          4000      100    
3         umer       Shopping Club   2000        
4         umer       The Store       1000        
5         umer       Rachna S/S      1500        
          umer       Royal Store               1000    
          umer       Chenab Store              1800    
          umer       Elite Mart                3500    
          umer       The Mart                  5000    

Presuming that the relation is based on salesman and shop then try something like 假设关系基于推销员和商店,然后尝试类似

SELECT 
    b.bill_no
    , b.saleman
    , b.shop
    , b.amount
    , l.credit
    , l.debit
FROM bill_status b
INNER JOIN ledger l
        ON l.saleman = b.saleman
       AND l.shop = b.shop

you can use Following. 您可以使用关注。

Select * from table1 INNER JOIN table2 ON table1_billno=table2_billno; 从table1 INNER JOIN table2中选择* ON table1_billno = table2_billno;

You can use this query to get the data from both table where bill no of both table are same. 您可以使用此查询从两个表(其中两个表的帐单号相同)中获取数据。 You got my Point? 你明白我的意思了吗?

You are looking of UNION: 您正在寻找UNION:

SELECT *, '' as credit, '' as debit
FROM bill_results

UNION ALL

SELECT *, '' as amount
FROM ledger

Make sure the tables has the same columns 确保表格具有相同的列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM