简体   繁体   English

如何在Mysql中实现Join以获取以下记录

[英]How to Implement Join In Mysql to Fetch the Following Record

I am new to mysql I have three tables as follows 我是mysql的新手,有以下三个表

Table No 1 purchase 表1购买

 +--------+-----------+--------+
 | UserID | productID |  traID |
 +--------+-----------+--------+
 | 525    |    2      |  602   |
 +--------+-----------+--------+
 | 525    |   1001    |  602   |
 +--------+-----------+--------+
 | 525    |   1002    |  602   |
 +--------+-----------+--------+
 | 525    |    1      |  602   |
 +--------+-----------+--------+

Table No 2 Deals 表2交易

 +--------+-----------+
 | dealsid| deals_name|
 +--------+-----------+
 |   1    | First Deal|
 +--------+-----------+
 |   2    |Second Deal|
 +--------+-----------+

Table No 3 Productmaster 表3产品负责人

 +-----------+--------------+
 | productID | product_name |
 +-----------+--------------+
 | 1001      | HTML         |
 +-----------+--------------+
 | 1002      | JAVA         |
 +-----------+--------------+

I want the output of the above table as follows 我想要上表的输出如下

(First Deal , Second Deal , HTML, JAVA) (第一笔交易,第二笔交易,HTML,JAVA)

The where Clause can be implemented to purchase.traID = 602 as I have posted on the above table. 正如我在上表中所发布的,可以在哪里购买该条款。

Please help me with some mysql query. 请帮我一些MySQL查询。

Try this: 尝试这个:

SELECT * 
FROM   purchase p, 
       deals d, 
       productmaster pm 
WHERE  p.productid = d.dealsid 
        OR p.productid = pm.productid AND p.traID = 602;

Try this, 尝试这个,

SELECT pm.product_name , d.deals_name
FROM   purchase p, 
       deals d, 
       productmaster pm 
WHERE  p.productid = d.dealsid 
        OR p.productid = pm.productid 
       AND p.traID = 602;

you can use SELECT distinct instead of select I will suggest to use Stored procedure: Find result from either table and get union of it as output 您可以使用SELECT distinct而不是select我建议使用存储过程:从任一表中查找结果并将其并集作为输出

CREATE PROCEDURE sp_get_product_name
(
arg_traid int
)
BEGIN
    Select pm.product_name 
    from productmaster pm 
    inner join  purchase p
    on pm.productID = p.productID
    where p.traID = arg_traid
    Union
    Select d.deals_name
    from deals d
    inner join purchase p
    on d.dealsID = p.productID
    where p.traID = arg_traid

END

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

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