I need to replicate following where condition in Oracle to mysql.. I have also written my version of mysql query and I am having trouble writing left outer join part (I need to perform left outer on voters and join with nodes. Can someone please help?
Oracle equivalent:
WHERE B.nid=C.nid
AND B.uid = D.uid
and D.nid(+)= E.nid
Mysql query
select
C.ccc
,D.dewde
from
USERS B
inner join
ACCOUNT_GROUP_INFO C on (B.nid = C.nid)
inner join
VOTERS D on (B.uid = D.uid)
left outer join
NODES E on (D.nid = E.nid)
Can someone please help me with the query?
Related
I wrote a query that returns 1 row when I run it in MySQL Workbench:
SELECT lot_num, block_num, base_price, SUM(price) AS options_price FROM lots AS l
INNER JOIN models AS m ON l.model_id = m.id
INNER JOIN lot_options AS lo ON l.id = lo.lot_id
INNER JOIN options AS o ON lo.option_id = o.id
GROUP BY l.id
When I try to run that in Jdbc I am getting 0 results. Here is how I have formatted the string for Jdbc:
SELECT lot_num, block_num, base_price, SUM(price) AS options_price FROM lots AS l INNER JOIN models AS m ON l.model_id = m.id INNER JOIN lot_options AS lo ON l.id = lo.lot_id INNER JOIN options AS o ON lo.option_id = o.id GROUP BY l.id;
This does not provide me with an error or anything. Just an empty result set.
I cannot figure out where I am going wrong with this query! Please bestow your SQL mastery upon me!
I figured it out. I didn't know enough about JOIN statements. The many-to-many tables need LEFT OUTER joins. I have no idea why my original query worked in Workbench, but not in my Jdbc connection. This works in both!
SELECT lot_num, block_num, models.base_price, SUM(o.price) FROM lots
INNER JOIN models ON models.id = lots.model_id
LEFT OUTER JOIN lot_options AS lo ON lots.id = lo.lot_id
LEFT OUTER JOIN options AS o ON lo.option_id = o.id
GROUP BY lots.id
It's to confusing, I have query where I have write JOIN with multiple table then which type of join it'll perform..?
For example :
SELECT
b.*
FROM
tbl_bookings b
JOIN tbl_users ua ON ua.id = b.act_id
JOIN tbl_users uc ON uc.id = b.cust_id
JOIN tbl_venue v ON b.venue_id = v.venue_id
WHERE
b.act_id = 4
Can any one please let me know by which type of join it'll perform..?
JOIN equals to an INNER JOIN . They are functionally equivalent, but INNER JOIN can be a bit clearer to read, especially if the query contains other type of JOIN
For something other than INNER JOIN you should specify the join you want.
LEFT JOIN / RIGHT JOIN which are the same as LEFT OUTER JOIN and RIGHT OUTER JOIN .
Those are just different ways of saying the same thing.
It will perform INNER JOIN. It is good to write INNER JOIN when you have different types of joins in query.
FROM (((Project
INNER JOIN MDS ON Project.PID=MDS.PID)
INNER JOIN PLocation ON Project.PID = PLocation.PID)
INNER JOIN Site ON PLocation.ACode = Site.ACode) AS [Prim]
LEFT JOIN ((Procurement INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum)
INNER JOIN DO ON DO.DoNum = MagicT.DONum) AS [Prim2] ON Prim.PRNum = Prim2.PRNum
Hey guys. So the above from statement is giving me an error:
Syntax error in from clause
Funny thing is when I make the LEFT JOIN an INNER JOIN it runs fine, but sadly that isn't what I want. I read that I had to rename the inner queries and join them using their new names but unfortunately I do not think I did it properly.
Maybe this... using two inline views. but you should be able to do this with just ()'s in Access... however if you have same named columns in each of Prim/prim2 tables, this will present with errors you may have to spell out each of the column names needed in the inline views.
FROM (SELECT * FROM Project
INNER JOIN MDS ON Project.PID=MDS.PID
INNER JOIN PLocation ON Project.PID = PLocation.PID
INNER JOIN Site ON PLocation.ACode = Site.ACode) PRIM
LEFT JOIN (SELECT * from Procurement
INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum
INNER JOIN DO ON DO.DoNum = MagicT.DONum) PRIM2
ON Prim.PRNUM = Prim2.PRNUM
--- maybe... in access the ()'s will handle the outer join but I don't know what table PRNUM is sourced from in (project, mds, plocation,site) so project is a guess...
FROM Project
INNER JOIN MDS ON Project.PID=MDS.PID
INNER JOIN PLocation ON Project.PID = PLocation.PID
INNER JOIN Site ON PLocation.ACode = Site.ACode
LEFT JOIN (SELECT * from Procurement
INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum
INNER JOIN DO ON DO.DoNum = MagicT.DONum) PRIM2
ON Project.PRNUM = Prim2.PRNUM
You basically have
FROM Subquery1 LEFT JOIN Subquery2
but your subqueries don't have SELECT in them.
I think it should be:
FROM
(SELECT Prim.PRNum FROM
(((Project
INNER JOIN MDS ON Project.PID=MDS.PID)
INNER JOIN PLocation ON Project.PID = PLocation.PID)
INNER JOIN Site ON PLocation.ACode = Site.ACode)
AS [Prim])
LEFT JOIN
(SELECT Prim2.PRNum FROM
((Procurement INNER JOIN MagicT ON Procurement.PRNum = MagicT.PRNum)
INNER JOIN DO ON DO.DoNum = MagicT.DONum)
AS [Prim2])
ON Prim.PRNum = Prim2.PRNum
(edit: a little too late...)
I have also discovered one of my issues here, is that in Access, you can have a left or right join nested inside an inner join but CANNOT have an inner join inside a left or right join. Rewriting the query to not include these joins inside the inner joins has solved the issue.
Thank you for all the help.
I am trying to do an inner join on four tables in MySQL. The table names are:
question (id_question, question_text, id_standard)
standard (id_standard, standardtext)
organisation_standard(id_organisation,id_organisation,id_standard)
organisation (id_organisation, organisation_name)
This is my query and it's giving me repetitive values:
select distinct a.question_text, d.organisation_name
from question a
inner join standard b on a.id_standard = b.id_standard
inner join organisation_standard c on b.id_standard= c.id_standard
inner join organisation d on c.id_organisation = d.id_organisation
where a.id_standard = 18;
How can I avoid the repetitive values?
What you need is a left join and not an inner join change the inner joins into left joins and you will get just one row:
select distinct
a.question_text, d.organisation_name
from
question a
left join
standard b ON a.id_standard = b.id_standard
left join
organisation_standard c ON b.id_standard = c.id_standard
left join
organisation d ON c.id_organisation = d.id_organisation
where
a.id_standard = 18
group by a.id_standard;
This diagram from another so answer gives the difference between the different joins
I have the following schema.
I can run two queries fairly simply
select * from booking_model_assignment
join booking_model on booking_model_assignment.booking_model_id = booking_model.id
left outer join axis_channel_mappings on bmi_id = axis_channel_mappings.assignment_id
left outer join axis_revenue_stream_mappings on bmi_id = axis_revenue_stream_mappings.assignment_id
which will give me all of the combinations of channel mappings and 'revenue_stream_mappings' which fit a booking model, with Null if there is one which only matches in one of the tables.
The other query
select * from axis_channel join axis_revenue_stream
Gives all of the possible combinations of channels and revenue streams.
What I would like is a query which will give all of the combinations, and the booking_model if that combination matches.
Any time I try to join or subquery I seem to get too many, or too few results. I think the issue is that I want the assignment_id to match across outer joins but only if there is an outer join.
The schema is laid out like this so it will be possible to add new axis and fit models to combinations, so if there is an easier way to achieve this I would be open to changing the schema.
EDIT
I have a partial solution based on Eggyal's answer but it is not extendable.
SELECT c.*, r.*, GROUP_CONCAT(a.bmi_id), GROUP_CONCAT(b.name) AS booking_models
FROM axis_channel c
CROSS JOIN axis_revenue_stream r
LEFT JOIN axis_channel_mappings cm ON cm.channel_id = c.id
LEFT JOIN axis_revenue_stream_mappings rm ON rm.revenue_stream_id = r.id
LEFT JOIN booking_model_assignment a ON (a.bmi_id = cm.assignment_id
AND a.bmi_id = rm.assignment_id)
OR (a.bmi_id = cm.assignment_id
AND rm.assignment_id IS NULL)
OR (cm.assignment_id IS NULL
AND a.bmi_id = cm.assignment_id)
LEFT JOIN booking_model b ON b.id = a.booking_model_id
GROUP BY c.id, r.id
But if I were to add more axes this query would grow way to cumbersome.
SELECT c.*, r.*, GROUP_CONCAT(b.name) AS booking_models
FROM axis_channel c
CROSS JOIN axis_revenue_stream r
LEFT JOIN axis_channel_mappings cm ON cm.channel_id = c.id
LEFT JOIN axis_revenue_stream_mappings rm ON rm.revenue_stream_id = r.id
LEFT JOIN booking_model_assignment a ON a.bmi_id = cm.assignment_id
AND a.bmi_id = rm.assignment_id
LEFT JOIN booking_model b ON b.id = a.booking_model_id
GROUP BY c.id, r.id