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
Related
I have a query where I am using a full outer join. But in some instance,
it gives me a syntax error.
What could be the reason for this? I don't see any miscode in my query.
MySQL does not support full outer join, but you can simulate it as a union between a left and right join query:
SELECT * FROM pbsdev3.item t1
LEFT JOIN pbsdev3.item_ledger_entry t2 ON t1.No_ = t2.Item_No_
UNION ALL
SELECT * FROM pbsdev3.item t1
RIGHT JOIN pbsdev3.item_ledger_entry t2 ON t1.No_ = t2.Item_No_
WHERE t1.No_ IS NULL
Note that in general if you find yourself doing full outer joins often, it could imply that your keys and data model are not well defined. One reason why MySQL does not support full joins could be that you should not have to use it.
Full outer join is quite a pain in MySQL. The first thing I would note is that it should not be needed. The items should match in the two tables, so an inner join or left join should be sufficient:
SELECT i.*, ile.*
FROM pbsdev3.item i LEFT JOIN
pbsdev3.item_ledger_entry ile
ON i.No_ = ile.Item_No_;
If you really need full outer join, then gather together all the items and use left join:
select it.*, ile.*
from (select i.No_ from item i union
select ile.Item_No_ from item_ledger_entry ile
) i left join
item it
on it.No_ = i.No_ left join
item_ledger_entry ile
on ile.No = i.No;
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`
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
I have table place contains author_id and table place_user_relation contains columns user_id,place_id
I want to fetch `place.id` where `place_user_relation.user_id=1` or `place.author_id=1`.Problem is this criteria is in two tables. To my knowledge, I wrote a wrong query:
SELECT r.*,p.* FROM place_user_relation as r
JOIN place as p
ON p.place_id=r.place OR p.author_id=1
WHERE r.user_id=1
/*I also tried WHERE r.user_id=1 OR p.author_id=1*/
in which contains an OR in ON statement, seems I can't do that. What's the right way to do this?
Try this:
SELECT p.id
FROM place_user_relation r JOIN place p ON p.id = r.place_id
WHERE p.author_id = 1
OR r.user_id = 1
SELECT r.*,p.* FROM place_user_relation as r
JOIN place as p
ON p.place_id=r.place
WHERE r.user_id=1
union
SELECT r.*,p.* FROM place_user_relation as r, place as p
WHERE r.user_id=1 AND p.author_id=1
MySQL is probably just not liking the fact that one of the conditions in the ON clause is only referencing one of the tables. Try making it a CROSS JOIN and moving all of the conditions into the WHERE clause, and using TRUE as the ON clause (since ON is apparently required)...
SELECT r.*,p.* FROM place_user_relation as r
CROSS JOIN place as p
ON true
WHERE (p.place_id=r.place OR p.author_id=1) and r.user_id=1
The UNION solution suggested by #arnon-rotem-gal-oz should also work.
This is driving me nuts. I have two tables that I am attempting to preform a join on, usersXstats and usersXstats_alltime.
Both tables have the same columns: id, userId, statId, and value
What I am trying to do is
SELECT *
FROM usersXstats
FULL JOIN usersXstats_alltime
ON usersXstats.userId=usersXstats_alltime.userId
AND usersXstats.statId=usersXstats_alltime.statId
However this is returning
Unknown column 'usersXstats.userId' in 'on clause'
This query works just as expected when replacing FULL JOIN with LEFT JOIN, RIGHT JOIN, or INNER JOIN.
To make it easier to read initially I wrote the following query:
SELECT *
FROM usersXstats as uxs
FULL JOIN usersXstats_alltime as uxsat
ON uxs.userId=uxsat.userId
AND uxs.statId=uxsat.statId
Which returned a different error:
check the manual that corresponds to your MySQL server version for the right syntax to use near 'FULL JOIN usersXstats_alltime as uxsat ON uxs.userId=uxsat.userId AND uxs.statId' at line 1
What on earth am I doing wrong? Thanks in advance!
MySQL doesn't support FULL JOIN
http://en.wikipedia.org/wiki/Join_%28SQL%29#Full_outer_join
Take a look at this How to simulate FULL OUTER JOIN in MySQL.
It may helps.
FULL OUTER JOIN won't support in mysql.
You can emulate FULL OUTER JOIN using UNION (from MySQL 4.0.0 on):
with two tables usersXstats,usersXstats_alltime
SELECT * FROM usersXstats
LEFT JOIN usersXstats_alltime ON usersXstats.userId= usersXstats_alltime.userId
UNION
SELECT * FROM usersXstats
RIGHT JOIN usersXstats_alltime ON usersXstats.statId= usersXstats_alltime.statId
SELECT Person1.Firstname, Person2.State
FROM Person1
left JOIN Person2
ON Person1.PersonID=Person2.PersonID
UNION
SELECT Person1.Firstname, Person2.State
FROM Person1
right JOIN Person2
ON Person1.PersonID=Person2.PersonID;
is working superbly.
I don't know what the problem is but I was facing the same issue so here you can try this:
SELECT *
FROM usersXstats
Left JOIN usersXstats_alltime
ON usersXstats.userId=usersXstats_alltime.userId
Union
SELECT *
FROM usersXstats
RightJOIN usersXstats_alltime
ON usersXstats.userId=usersXstats_alltime.userId