How to select from two tables based on a where clause - mysql

I am trying to select from two tables and join them where they have the same id but my query seems wrong that it searches for so long that it crashed workbench.
SELECT s.id,
s.title,
s.votes,
t.*
FROM movies_two.movie s,
movies_one.movie t
LEFT JOIN movies_two.movie
ON movies_one.id = s.id -- not sure which join to use
WHERE s.votes = -1 AND
t.numofvotes > 0;
I have two databases containing similar data. I am trying to select rows that from movies_one and movies_two that have the same id and where movies_one has votes = -1 and movies_two has movies > 0

You don't need a left join for this. Nor do you need a cross join (which is what the comma does in the from clause.
I think you want something like this:
SELECT *
FROM movies_one.movie m1 JOIN
movies_two.movie m2
ON m1.id = m2.id AND
m1.votes = -1 AND
m2.numofvotes > 0;
I would also suggest that you use table aliases that are abbreviations for the table names. Your text description of what you wand and your query are quite different.

SELECT s.id ,s.title, s.votes, t.*
FROM movies_two.movie s
JOIN movies_one.movie t
ON t.id = s.id
WHERE s.votes =-1
AND t.numofvotes >0;
Would be okay.
You use a left join, when you only want the data in movies_two.movie - the left table. But here you want the data of both tables.

Try this:
SELECT *
FROM movies_one.movie
WHERE numofvotes = -1
UNION
SELECT *
FROM movies_two.movie
WHERE votes > 0

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.

Left join sql query

I want to get all the data from the users table & the last record associated with him from my connection_history table , it's working only when i don't add at the end of my query
ORDER BY contributions DESC
( When i add it , i have only the record wich come from users and not the last connection_history record)
My question is : how i can get the entires data ordered by contributions DESC
SELECT * FROM users LEFT JOIN connections_history ch ON users.id = ch.guid
AND EXISTS (SELECT 1
FROM connections_history ch1
WHERE ch.guid = ch1.guid
HAVING Max(ch1.date) = ch.date)
The order by should not affect the results that are returned. It only changes the ordering. You are probably getting what you want, just in an unexpected order. For instance, your query interface might be returning a fixed number of rows. Changing the order of the rows could make it look like the result set is different.
I will say that I find = to be more intuitive than EXISTS for this purpose:
SELECT *
FROM users u LEFT JOIN
connections_history ch
ON u.id = ch.guid AND
ch.date = (SELECT Max(ch1.date)
FROM connections_history ch1
WHERE ch.guid = ch1.guid
)
ORDER BY contributions DESC;
The reason is that the = is directly in the ON clause, so it is clear what the relationship between the tables is.
For your casual consideration, a different formatting of the original code. Note in particular the indented AND suggests the clause is part of the LEFT JOIN, which it is.
SELECT * FROM users
LEFT JOIN connections_history ch ON
users.id = ch.guid
AND EXISTS (SELECT 1
FROM connections_history ch1
WHERE ch.guid = ch1.guid
HAVING Max(ch1.date) = ch.date
)
We can use nested queries to first check for max_date for a given user and pass the list of guid to the nested query assuming all the users has at least one record in the connection history table otherwise you could use Left Join instead.
select B.*,X.* from users B JOIN (
select A.* from connection_history A
where A.guid = B.guid and A.date = (
select max(date) from connection_history where guid = B.guid) )X on
X.guid = B.guid
order by B.contributions DESC;

Subquery for SQL

I need assistance in joining the two query statements together using subquery. I am confused on how I can combine the two together. I appreciate the help.
SELECT * FROM MEDICAL_PROCEDURE
JOIN PROCEDURE_CATEGORY ON medical_procedure.procedure_category_id = PROCEDURE_CATEGORY.PROCEDURE_CATEGORY_ID;
SELECT
Medical_procedure.medical_procedure_id,
COUNT(procedure_tool_supply.medical_procedure_id) AS Supply_Needed
FROM Procedure_tool_supply
JOIN Medical_Procedure on Procedure_tool_supply.medical_procedure_id = Medical_procedure.medical_procedure_id
GROUP BY Procedure_tool_supply.medical_procedure_id
HAVING COUNT(Procedure_tool_supply.medical_procedure_id) < 3;
Can't really test without test data, but this should work. Hopefully I figured out correctly what you're trying to do:
SELECT *
FROM
MEDICAL_PROCEDURE P
JOIN PROCEDURE_CATEGORY C ON
P.procedure_category_id = C.PROCEDURE_CATEGORY_ID
cross apply (
SELECT
COUNT(T.medical_procedure_id) AS Supply_Needed
FROM
Procedure_tool_supply T
where
T.medical_procedure_id = P.medical_procedure_id
GROUP BY
T.medical_procedure_id
HAVING
COUNT(T.medical_procedure_id) < 3
) T
It's not clear what you are trying to achieve. But if your intent is to include the derived Supply_Needed column from the second query on each row from the first query, and to restrict the rows returned to those that have a medical_procedure_id value returned by the second query, then...
you could do something like this:
SELECT mp.*
, pc.*
, ct.Supply_Needed
FROM MEDICAL_PROCEDURE mp
JOIN PROCEDURE_CATEGORY pc
ON mp.procedure_category_id = pc.PROCEDURE_CATEGORY_ID
JOIN ( SELECT pr.medical_procedure_id
, COUNT(ts.medical_procedure_id) AS Supply_Needed
FROM Procedure_tool_supply ts
JOIN Medical_Procedure pr
ON ts.medical_procedure_id = pr.medical_procedure_id
GROUP BY ts.medical_procedure_id
HAVING COUNT(ts.medical_procedure_id) < 3
) ct
ON ct.medical_procedure_id = mp.medical_procedure_id

How to give alias to results returned after inner join in mySQL

I need to do a correlated SQL Query and for that purpose i need to provide an alias to outer query which in which I perform an inner join. I am not able to do the alias
SELECT DISTINCT(name)
FROM PERSON
INNER JOIN M_DIRECTOR AS dira
ON (dira.PID = M_DIRECTOR.PID) as dira
WHERE 9 > (
SELECT COUNT(MID) FROM M_DIRECTOR WHERE name = dira.name
) ;
I didn't really understand what you want to do, but I guess
select
distinct p.name,
count(d.MID) cnt
from
hindi2_PERSON p
inner join
hindi2_M_DIRECTOR d
on
p.PID = d.PID
group by
p.name
having count(d.MID) > 9
;
would do what you want
I dont know what you are asking and what you mean by make an alias to an eniter result ?
but you are doing
select distinct(name) as othername
which is you are selecting name and you are giving here othername as an alias
then you retrieve it in result
$row['othername']
There's still something missing. From what you write, there is a field name in the M_DIRECTOR table?
Please show all the tables and attributes involved, use an SQL Fiddle to prepare an example.
SELECT DISTINCT(name)
FROM PERSON as p
INNER JOIN (
SELECT COUNT(MID), PID FROM M_DIRECTOR WHERE name = dira.name
) as d
ON (p.PID = d.PID) ;

How to optimize this complected query?

While working with following query on mysql, Its getting locked,
SELECT event_list.*
FROM event_list
INNER JOIN members
ON members.profilenam=event_list.even_loc
WHERE (even_own IN (SELECT frd_id
FROM network
WHERE mem_id='911'
GROUP BY frd_id)
OR even_own = '911' )
AND event_list.even_active = 'y'
GROUP BY event_list.even_id
ORDER BY event_list.even_stat ASC
The Inner query inside IN constraint has many frd_id, So because of that above query is slooow..., So please help.
Thanks.
Try this:
SELECT el.*
FROM event_list el
INNER JOIN members m ON m.profilenam = el.even_loc
WHERE el.even_active = 'y' AND
(el.even_own = 911 OR EXISTS (SELECT 1 FROM network n WHERE n.mem_id=911 AND n.frd_id = el.even_own))
GROUP BY el.even_id
ORDER BY el.even_stat ASC
You don't need the GROUP BY on the inner query, that will be making the database engine do a lot of unneeded work.
If you put even_own = '911' before the select from network, then if even_own IS 911 then it will not have to do the subquery.
Also why do you have a group by on the subquery?
Also run explain plan top find out what is taking the time.
This might work better:
( SELECT e.*
FROM event_list AS e
INNER JOIN members AS m ON m.profilenam = e.even_loc
JOIN network AS n ON e.even_own = n.frd_id
WHERE n.mem_id = '911'
AND e.even_active = 'y'
ORDER BY e.even_stat ASC )
UNION DISTINCT
( SELECT e.*
FROM event_list AS e
INNER JOIN members AS m ON m.profilenam = e.even_loc
WHERE e.even_own = '911'
AND e.even_active = 'y' )
ORDER BY e.even_stat ASC
Since I don't know whether the JOINs one-to-many (or what), I threw in DISTINCT to avoid dups. There may be a better way, or it may be unnecessary (that is, UNION ALL).
Notice how I avoid two things that are performance killers:
OR -- turned into UNION
IN (SELECT...) -- turned into JOIN.
I made aliases to cut down on the clutter. I moved the ORDER BY outside the UNION (and added parens to make it work right).