How to select and join more than 2 tables in oracle? - oracle

I have an oracle database design as shown in the picture.
my question, how can i display id_produk of tb_produk by using select with condition :
produk_gaya.id_ghidup = wajah_gaya.id_ghidup
produk_konsern.id_konsern = wajah_konsern.id_konsern
produk_tipe.id_tipe = tb_wajah.id_tipe
Please help, thank you
image of database design

You appear to just want to join the tables along the primary and foreign key relationships (you can skip tb_hidup as produk_gaya and wajah_gaya both have foreign-key relationship to the same primary key; similar for tb_konsern and tb_type):
SELECT p.id_produk
FROM tb_produk p
INNER JOIN produk_gaya pg ON p.id_produk = pg.id_produk
INNER JOIN wajah_gaya wg ON pg.id_ghidup = wg.id_ghidup
INNER JOIN produk_konsern pk ON p.id_produk = pk.id_produk
INNER JOIN wajah_konsern wk ON pk.id_konsern = wk.id_konsern
INNER JOIN produk_tipe pt ON p.id_produk = pt.id_produk
INNER JOIN tb_wajah w ON pt.id_tipe = tb_wajah.id_tipe

Related

sql select records that don't have relation in a third table

I have three tables
CLAIMS_TB
CLAIMS_RISK_TB
VEHICLE_TB
And then I need this result below:
Who can help me or share with me the query to be used?
N.B: If the code is 700 it means that it is a vehicle and it must fill the column called "ai_vehicle_use" otherwise it must leave it blank because "VEHICLE_TB" table contains only vehicles
This is what I tried:
select
klm.CM_PL_INDEX,
klm.cm_no,
klmrisk.cr_risk_code,
CASE WHEN klm.CM_PR_CODE = '0700' THEN klmrisk.cr_risk_code ELSE '' END,
veh.ai_vehicle_use
from CLAIMS_TB klm
JOIN CLAIMS_RISK_TB klmrisk
ON (klm.cm_index = klmrisk.cr_cm_index)
INNER JOIN VEHICLE_TB veh
on veh.ai_regn_no = klm.cm_no
where klm.cm_no='CL/01/044/00001/01/2018'
or klmrisk.cr_cm_index='86594'
order by klmrisk.cr_risk_code;
I believe this could fit your needs.
SELECT
*
FROM CLAIMS_TB AS c
LEFT JOIN CLAIMS_RISK_TB cl ON c.cm_index = cl.cr_cm_index
LEFT JOIN VEHICLE_TB v ON cl.cr_risk_code = v.ai_risk_index
Finaly I find the solution, query below works:
select * from CLAIMS_TB c
JOIN CLAIMS_RISK_TB cr ON( C.CM_INDEX = cr.cr_cm_index)
LEFT OUTER JOIN VEHICLE_TB v ON (cr.cr_risk_code = v.ai_regn_no);

oracle: how to find the student met with prerequisite

I am a new in oracle. how to find the student met with prerequisite
SELECT
c.course_no,
c.description,
c.prerequisite,
level AS level_course,
stu.student_id
FROM
course c
INNER JOIN section s ON c.course_no = s.course_no
INNER JOIN enrollment e ON s.section_id = e.section_id
INNER JOIN student stu ON e.student_id = stu.student_id
CONNECT BY
PRIOR c.course_no = c.prerequisite
[1]: https://i.stack.imgur.com/SNG0G.png COURSE TABLE
[1]: https://i.stack.imgur.com/Asy8F.png ENROLLMENT TABLE https://i.stack.imgur.com/1axEi.png SECTION TABLE
You might need to add START WITH before CONNECT BY.
Here take course_no 20 for example.
You can remove AND c.course_no = 20 to see about all the courses.
Note: also adjusted the columns to make it more clear
SELECT
level AS level_course,
c.course_no,
e.section_id,
c.prerequisite,
c.description,
stu.student_id
FROM
course c
INNER JOIN section s ON c.course_no = s.course_no
INNER JOIN enrollment e ON s.section_id = e.section_id
INNER JOIN student stu ON e.student_id = stu.student_id
START WITH c.prerequisite IS NULL AND c.course_no = 20
CONNECT BY
PRIOR c.course_no = c.prerequisite

How can I solve this CRUD set_relation_n_n issue

I have three tables:
aauth_user: id,email,pass,name,banned,last_login
aauth_user_to_groups: user_id,group_id
aauth_groups: id,name,definition
Here I want to set relation table auth_user_to_groups with auth_groups.
Here auth_user is basic table and auth_user_to_groups is relational table and auth_groups is selection table
try this:
SELECT *
FROM aauth_user_to_groups a
JOIN aauth_user b ON a.user_id = b.id
JOIN aauth_groups c ON a.group_id = c.id ;

Oracle SQL developer how to display NULL value of the foreign key

I am going to try to use left outer join between Ticket and Membership.
However, it does not display the foreign key of NULL values on Ticket. Could you give me some answer for this what's wrong with this query?
Thanks.
FROM Ticket t, Production pro, Performance per, Price, Price_level, Booking, Customer, Customer_Concession ccons, Membership, Member_concession mcons
WHERE t.performanceid = per.performanceid AND
t.PRODUCTIONID = Price.PRODUCTIONID AND
t.levelId = Price.levelId AND
Price.PRODUCTIONID = pro.PRODUCTIONID AND
Price.levelId = Price_level.levelId AND
Booking.bookingId (+) = t.bookingId AND
Customer.customerId = Booking.customerId AND
ccons.cConcessionId (+) = Customer.cConcessionId AND
Membership.membershipId (+) = t.membershipId AND
Membership.mConcessionId = mcons.mConcessionId
ORDER BY t.ticketId
One potential problem you have is these two conditions:
Booking.bookingId (+) = t.bookingId AND
Customer.customerId = Booking.customerId AND
Since you're doing an outer join to Booking, its columns will appear as NULL when no match is found; but then your doing a normal join to Customer, so those rows will be eliminated since NULL cannot be equal to anything. You may want to change the second line to an outer join as well.
But, I don't know if that's your primary problem, since I don't actually understand exactly what you're asking. What do you mean by "NULL value of the foreign key"? You haven't specified what your foreign keys are.
To expand on Dave's observation, and to give you an example of SQL92 syntax, please please please learn it and get away from Oracle's own outer join syntax.
FROM
TICKET t
JOIN Performance per
ON per.performance_id = t.performance_id
JOIN Production pro
ON pro.produciton_id = t.production_id
JOIN PRICE pr
ON pr.production_id = pro.production_id
AND pr.levelId = t.level_id
JOIN price_level pl
ON pl.levelid = pr.levelid
LEFT OUTER JOIN booking b
on b.booking_id = t.booking_id
LEFT OUTER JOIN customer cus
on cus.customer_id = b.customer_id
LEFT OUTER JOIN customer_concession cons
ON cons.concession_id = cus.concession_id
LEFT OUTER JOIN memebership m
ON M.membership_id = t.membership_id
LEFT OUTER JOIN membership_concession mc
ON mc.mConcession_id = m.mConcession_id
Order by t.ticketid

Why is LINQ-to-Entities is generating an extra left outer join

Background
I have 2 tables: OrderItem and OrderItemValue. An OrderItem can have 0 or 1 OrderItemPackageValue. The Primary key in both tables is the same and there is a foreign key reference from OrderItems to OrderItemPackageValue
LINQ
var dummy =
from orderItem in context.OrderItems
select new
{
OrderID = orderItem.OrderID,
LineNumber = orderItem.LineNumber,
PackageValue = (decimal?)orderItem.OrderItemPackageValue.PackageValue
};
var dummy2 = dummy.ToArray();
SQL
SELECT
[Extent1].[OrderID] AS [OrderID],
[Extent1].[LineNumber] AS [LineNumber],
[Extent3].[PackageValue] AS [PackageValue]
FROM [dbo].[OrderItems] AS [Extent1]
LEFT OUTER JOIN [dbo].[OrderItemPackageValues] AS [Extent2] ON ([Extent1].[OrderID] = [Extent2].[OrderID]) AND ([Extent1].[LineNumber] = [Extent2].[LineNumber])
LEFT OUTER JOIN [dbo].[OrderItemPackageValues] AS [Extent3] ON ([Extent2].[OrderID] = [Extent3].[OrderID]) AND ([Extent2].[LineNumber] = [Extent3].[LineNumber])
I'm trying to get this via Linq2Entities (MSSQL) and it seems to be generating an extra outer join, and I can't figure out how to get rid of it.
This is a bug; see here and here.
Workaround: Try changing your relationship from 1 : Many to 0..1 : Many

Resources