简体   繁体   中英

How to create a view in mysql by mapping 3 tables?

1st table refferal_benifits

2nd table puja_orders_details

From the user I will get the puja_order_id in the first table.. I need to map it with 2nd table and I will get puja_id.. Then I need to map puja_id with 3rd table named(pujas) and I will get puja_name.. Here I should create 2 views.. Instead of this how to map 3 tables and get only 1 view??

Okay, try this view query:

CREATE VIEW your_view_name AS

SELECT * FROM refferal_benifits t1 INNER JOIN puja_order_details t2
ON t1.puja_order_id = t2.puja_order_id
INNER JOIN pujas t3
ON t2.puja_id = t3.puja_id
WHERE t1.puja_order_id = your_id_that_you_want

To create a view by three tables 'orders', 'customer' and ' agents' with following conditions -

1. 'a' and 'b' and 'c' are the aliases of 'orders' and 'customer' and 'agents' table,

2. 'cust_code' of 'orders' and 'customer' table must be same,

3. 'agent_code' of 'orders' and 'agents' table must be same,

the following SQL statement can be used :

CREATE VIEW ordersview  
AS SELECT ord_num, ord_amount, a.agent_code,  
agent_name, cust_name  
FROM orders a, customer b, agents c  
WHERE a.cust_code = b.cust_code  
AND a.agent_code = c.agent_code;  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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