How to apply OR to JOIN...ON statement - mysql

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.

Related

Mysql - I have 3 unique tables, need a hint on getting the details with the count

I have 3 tables which are interconnected and i want to select columns from two tables and counts from table 3. If anyone is aware on this, any hint would be appreciated.
Below is the sql i tried, but the count is getting repeated
SELECT distinct p.p_id, p.p_f6, p.p_l4,m.m_id, (
SELECT COUNT(*)
FROM ttokens t where t.pdetail_id = p.pdetail_id
) AS token_count
FROM tparking p,ttokens t LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
WHERE t.pdetail_id = p.pdetail_id
You can try to use JOIN with subquery to get your count instead of selcet subquery.
SELECT p.p_id, p.p_f6, p.p_l4,m.m_id,t.cnt
FROM tparking p
JOIN (
SELECT pdetail_id,COUNT(*) cnt
FROM ttokens
GROUP BY pdetail_id
) t ON t.pdetail_id = p.pdetail_id
LEFT join ttokens_md m ON t.trefn_id = m.trefn_id
Note
I would use JOIN instead of , comma with where condition to connect two tables,, is an old style.

how to join SQL with 3 tables

I'm trying to study SQL.
I have a problem with JOIN
I want to display ref_id, pro_name, class_name but I couldn't.
I find EFFICIENT solution.
MY QUERY (DOESN'T WORK)
SELECT
ref_id, pro_name, class_name
FROM
RC, RP, PP, LP
WHERE
RC.ref_id = RP.ref_id
Avoid using commas be CROSS JOIN
You could use JOIN to instead of commas
like this.
SELECT
RP.ref_id, PP.pro_name, LP.class_name
FROM
RP
LEFT JOIN RC ON RC.ref_id = RP.ref_id
LEFT JOIN PP ON PP.pro_id = RP.pro_id
LEFT JOIN LP ON LP.lec_id = RP.lec_id
Never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax.
You would seem to want:
select rp.pro_id, pp.pro_name, lp.class_name
from rp left join
pp
on rp.pro_id = pp.pro_id left join
lp
on rp.lec_id = lp.lec_id;
Note the use of left join. This ensure that all rows are in the result set, even when one or the other joins doesn't find a matching record.
From what I can see, the table rc is not needed to answer this specific question.

SQL Join Issues Microsoft Query

I've been trying to write some code in SQL, but it keeps coming up with a syntax error regarding the join, and I can't work out why.
SELECT `COUNTRY$`.country_name, `PARTNER$`.partner_name, count(member_id)
FROM `Member$`
Left Join `COUNTRY$`
ON `MEMBER$`.country_id=`COUNTRY$`.country_id
lEFT jOIN `PARTNER$`
on `MEMBER$`.partner_ID = `PARTNER$`.partner_ID
Group By country_name,Partner_name
Any help would be appreciated.
May have something to do with how your table names are in 'thisFormat$'. Also you did not specify which table member_id was coming and group by also doesn't specify which table country_name, partner_name was from.
Try putting aliases on the tablenames and see if that eliminates the problem
SELECT c.country_name, p.partner_name, count(m.member_id)
FROM `Member$` m
left join `COUNTRY$` c on c.country_id = m.country_id
left join `PARTNER$` p on p.partner_id = m.partner_id
GROUP BY c.country_name, p.partner_name

MySQL: why does GROUP_CONCAT lose rows in my multiple join query?

I'm trying to fetch all the rows from table_m which also have an index in table_mi and I'm expecting to get 2 rows as a result (with m.id=3 and m.id=9) - but if I add GROUP_CONCAT to my select then I only get one row returned. Am I having a misshap somewhere within those joins of mine?
Query:
SELECT
m.id,
m.name,
m.keyword,
IFNULL(GROUP_CONCAT(r.keyword),'TEST') AS restrictions
FROM
table_m AS m
INNER JOIN
table_mi as mi ON m.id=mi.m_id
LEFT JOIN
table_ri as ri ON m.id=ri.m_id
LEFT JOIN
table_r AS r ON ri.r_id=r.id
WHERE
(
m.id>0
AND m.active=1
AND mi.p_id=0
AND (mi.pa_id="11" OR (mi.pa_id=0 AND mi.id!=0))
AND mi.u_id=IF((SELECT id FROM table_mi WHERE p_id=0 AND pa_id="11" AND u_id="2")>0,"2",0)
) OR mi.id=0
ORDER BY
mi.priority;
This is what I'm getting as a result:
ID NAME KEYWORD RESTRICTIONS
9 test_a key_a r_key_2,r_key_3,r_key_4
This is what I'm expecting:
ID NAME KEYWORD RESTRICTIONS
9 test_a key_a r_key_2,r_key_3,r_key_4
3 test_b key_b TEST
Please see my full example with schema on sql fiddle: http://sqlfiddle.com/#!2/359d9/1
GROUP_CONCAT is an aggregate function. It will bring back a single row UNLESS you specify a GROUP BY clause (with any fields that are not in the GROUP BY being aggregate fields)
Before the ORDER BY add the following:-
GROUP BY m.id, m.name, m.keyword
That said it looks like you might want to use CONCAT to join 2 values together rather than GROUP_CONCAT
As an aside, your SQL might be easier to read if you eliminate the subselect. Assuming it is bringing back a single record then possibly as follows
SELECT
m.id,
m.name,
m.keyword,
IFNULL(GROUP_CONCAT(r.keyword),'TEST') AS restrictions
FROM
table_m AS m
INNER JOIN
table_mi as mi ON m.id=mi.m_id
LEFT JOIN
table_ri as ri ON m.id=ri.m_id
LEFT JOIN
table_r AS r ON ri.r_id=r.id
LEFT OUTER JOIN
table_mi AS mi2 ON mi2.p_id=0 AND mi2.pa_id="11" AND mi2.u_id="2"
WHERE
(
m.id>0
AND m.active=1
AND mi.p_id=0
AND (mi.pa_id="11" OR (mi.pa_id=0 AND mi.id!=0))
AND mi.u_id=IF(mi2.id >0,"2",0)
) OR mi.id=0
ORDER BY
mi.priority;
You do no need GROUP_CONCAT to achieve what you want.
Instead of :
IFNULL(GROUP_CONCAT(r.keyword),'TEST') AS restrictions
use
IFNULL(r.keyword,'TEST') AS restrictions
OR:
Keep the query as it is and add GROUP BY m.id before ORDER BY

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