INNER JOIN on selection - mysql

I have a problem with the inner join. I need to filter the table before it will combine tables using inner join. The following code is of course wrong but I need something like this. Please help.
SELECT * FROM hotel LEFT OUTER JOIN (**hotel_table where hotel_table.id_offer=$id**) ON (hotel.id = hotel_table.id_hotel)
Edit:
Thanks for the fast answer. But I get an error: "Every derived table must have its own alias" My code:
SELECT *
FROM
czajka_zakwaterowania
LEFT OUTER JOIN
(
SELECT *
FROM czajka_hotel_l
WHERE czajka_hotel_l.id_hotel=$id
)
ON (czajka_zakwaterowania.id = czajka_hotel_l.id_hotel)
How do I solve this?
This code displays the error: "Every derived table must have its own alias" I cant eliminate it.

SELECT *
FROM
hotel
LEFT OUTER JOIN
hotel_table ON hotel.id = hotel_table.id_hotel AND hotel_table.id_offer=$id
or
SELECT *
FROM
hotel
LEFT OUTER JOIN
(
SELECT *
FROM hotel_table
WHERE hotel_table.id_offer=$id
) ht ON hotel.id = ht.id_hotel

Related

How to extract an original table from a join in sql

with this database I did the next query :
WITH comp_courses AS
(SELECT * FROM course WHERE title LIKE "Comp%")
SELECT * FROM (instructor INNER JOIN (comp_courses INNER JOIN teaches USING(course_id)) USING(ID));
now that returns a join of a bunch of tables. But what I want is only the colmuns from that query that appear in instructor. Any ideas on how to do it?
All you would have to do is select all the columns from that table using the * operator like so:
instructor.*
This should get you the result your looking for:
SELECT
instructor.*
FROM
instructor AS `i`
JOIN teaches AS `t` ON `t`.id = `c`.id
JOIN course AS `c` ON `c`.course_id = `t`.course_id
WHERE
`c`.title LIKE 'Comp%';
Hope this helps,

Problem when using full join but not using left join. Why?

When I use a left join on different databases, it works but not when I use inner join. Why ?
SELECT tkblue_tklabel_dev_data.EmailContent.*, tkblue_tklabel_dev_archdata.EmailTracking.*
FROM tkblue_tklabel_dev_data.EmailContent
FULL JOIN tkblue_tklabel_dev_archdata.EmailTracking ON tkblue_tklabel_dev_archdata.EmailTracking.idEmailTracking = tkblue_tklabel_dev_data.EmailContent.idEmailTracking
Unknown table 'tkblue_tklabel_dev_data.EmailContent'
But when using
SELECT tkblue_tklabel_dev_data.EmailContent.*, tkblue_tklabel_dev_archdata.EmailTracking.*
FROM tkblue_tklabel_dev_data.EmailContent
LEFT JOIN tkblue_tklabel_dev_archdata.EmailTracking ON tkblue_tklabel_dev_archdata.EmailTracking.idEmailTracking =
tkblue_tklabel_dev_data.EmailContent.idEmailTracking
I have not this error message, but I can only have the results of the left table. Or, I wish all the results like an full join.
Full join don't exists in mysql but you can produce the same result using both left join and right join in UNION
SELECT tkblue_tklabel_dev_data.EmailContent.*
, tkblue_tklabel_dev_archdata.EmailTracking.*
FROM tkblue_tklabel_dev_data.EmailContent
LEFT JOIN tkblue_tklabel_dev_archdata.EmailTracking
ON tkblue_tklabel_dev_archdata.EmailTracking.idEmailTracking = tkblue_tklabel_dev_data.EmailContent.idEmailTracking
UNION
SELECT tkblue_tklabel_dev_data.EmailContent.*
, tkblue_tklabel_dev_archdata.EmailTracking.*
FROM tkblue_tklabel_dev_data.EmailContent
RIGHT JOIN tkblue_tklabel_dev_archdata.EmailTracking
ON tkblue_tklabel_dev_archdata.EmailTracking.idEmailTracking = tkblue_tklabel_dev_data.EmailContent.idEmailTracking

MS Access Multi-Join Query

Can anyone find what is wrong with this MS Access Query? When I try to execute it i receive an error about a missing Operator before the 2nd Left Join
SELECT * FROM (
SELECT GetitUsageTemp.MemberID,
GetitUsageTemp.IDNumber,
GetitUsageTemp.Title,
GetitUsageTemp.Initials,
GetitUsageTemp.Forenames,
GetitUsageTemp.Surnames,
GetitUsageTemp.CellNumber,
GetitUsageTemp.EmailAddress,
Nz(August.[AugustUsage],0) AS AugustUsage
FROM GetitUsageTemp
LEFT JOIN
(SELECT dbo_Requests.fk_Members_ID, Count(dbo_Requests.Log_date) AS JulyUsage
FROM dbo_Requests
WHERE dbo_Requests.Log_date Between #07/01/2013# And #08/01/2013#
GROUP BY dbo_Requests.fk_Members_ID
) Requests
ON GetitUsageTemp.MemberID = Requests.fk_Members_ID
LEFT JOIN
(SELECT dbo_Requests.fk_Members_ID, Count(dbo_Requests.Log_date) AS AugustUsage
FROM dbo_Requests
WHERE dbo_Requests.Log_date Between #08/01/2013# And #09/01/2013#
GROUP BY dbo_Requests.fk_Members_ID
) August
ON GetitUsageTemp.MemberID = August.fk_Members_ID
)GETIT
In Access you can only join two tables. If you need to join more tables, you need to group the first join together using parentheses, as if it was a new derived table. Then you can join another table to that group:
select
*
from
( ( Table1
LEFT JOIN Table2 ...
)
LEFT JOIN Table3 ...
)
LEFT JOIN Table4 ...
(I'm using awkward indentation to try to make the groups more clear)

SQL aliases for subquery

I have a problem with following query:
SELECT * FROM (
(SELECT * FROM Images
WHERE create_user_id=:user_id) RIGHT INNER JOIN
(SELECT * FROM PhotoGallery) ON id=centity_id
)
ORDER BY centity_id;
I am getting 1248 - Every derived table must have its own alias error and I know I need to give those tables aliases, but whatever I do, I always get error. Could anyone help me to solve this? Thank you very much!
Your subqueries need an alias. Something like:
SELECT * FROM (
(SELECT *
FROM Images
WHERE create_user_id=:user_id
) i RIGHT INNER JOIN
PhotoGallery pg
ON i.id=pg.centity_id
)
ORDER BY centity_id;
I've never heard of a dbms that supported "RIGHT INNER JOIN". Choose one of
INNER JOIN, or
RIGHT OUTER JOIN, or
RIGHT JOIN (which should mean the same as RIGHT OUTER JOIN).
If you are doing a select *, you don't need to do a sub-query.
SELECT * FROM Images
RIGHT OUTER JOIN PhotoGallery ON id=centity_id
WHERE create_user_id = :user_id
ORDER BY centity_id;
Try this
SELECT *
FROM Images i RIGHT JOIN
PhotoGallery p ON i.id=p.centity_id
WHERE i.create_user_id=:user_id
ORDER BY p.centity_id
Based on your DDL and your desired output join might be RIGHT, INNER or LEFT, but not RIGHT INNER JOIN

MySQL Select ALL from Left Join

This query does not work. I want all the results from a LEFT JOIN where something is something. This is my exact code:
SELECT * FROM `swarovski_zones` WHERE `siteid`='200'
LEFT JOIN `trafficviews` ON `swarovski`.`id`=`trafficviews`.`adid`
swarovski_zones table is siteid 200
trafficviews table is adid 200
The 200 is the linking variable between the tables. I want everything from both tables where the ID is 200.
The query doesn't work because the syntax is incorrect. It should be:
select
from
join
on
where
group by
having
order by
limit
Giving you:
select *
from `swarovski_zones`
left join `trafficviews`
on `swarovski`.`id` = `trafficviews`.`adid`
where `siteid` = '200'
Also is siteid meant to be a string and not an integer?
I'm probably going to regret providing the list above...
Limit! I forgot Limit; the full syntax list is here
The problem here is that elements in your right table (trafficviews) might not have a correspondant row in your left table (swarovski_zones). So the left join will get all elements from the left and might leave out some elements from the right.
To solve this, you need an outer join. Your problem is that MySQL does not support outer joins :) This is solved in the following general way:
SELECT * FROM a LEFT JOIN b ON a.id = b.id
UNION ALL
SELECT * FROM a RIGHT JOIN b ON a.id = b.id WHERE a.id IS NULL;
Applied to your question this should be something like:
SELECT * FROM swarovski_zones s
LEFT JOIN trafficviews ON s.id = t.adid
UNION ALL
SELECT * FROM swarovski_zones s
RIGHT JOIN trafficviews ON s.id = t.adid WHERE s.id IS NULL
WHERE s.siteid = 200 or t.adid = 200
Give it a try.
User full outer join
SELECT * FROM `swarovski_zones` WHERE `siteid`='200'
FULL OUTER JOIN `trafficviews` ON `swarovski`.`id`=`trafficviews`.`adid`