MYSQL get spacific table fields counts - mysql

I have the following 2 tables.
First is: idea_box
Second is: idea_box_voting
Now I want result something like this, based on 0 and 1 countfrom thumbs field.
I have never created sql query like this one so I am hoping that someone help me.
Thanks.

Try this:
SELECT a.idea_id, a.property_id, a.the_idea, a.user_id, a.added_date, a.status,
SUM(b.thumbs = 1) AS up, SUM(b.thumbs = 0) AS down
FROM idea_box a
LEFT JOIN idea_box_voting b ON a.idea_id = b.idea_id
GROUP BY a.idea_id;

Try this:
SELECT ib.*,
SUM(ibv.thumbs=1) as UP,
SUM(ibv.thumbs=0) as DOWN
FROM idea_box ib LEFT JOIN
idea_box_voting ibv on ib.idea_id=ibv.idea_id
GROUP BY ib.idea_id
An example in SQL Fiddle.

SELECT t1.*,sum(t2.thumbs=1) as up,sum(t2.thumbs=0) as down FROM table1 as t1 left join table2 as t2 using(idea_id) group by t1.idea_id;

Related

Mysql query join table and let fill with empty if value same

I Have problem with joining 2 table
this is mysql query
select *
from tbl_perspective a
inner join tbl_objective b on b.idperspective=a.idperspective
The result is:
Query Result
I Want to display first row of perspectivename and blank or null
Final Result:
enter image description here
Hi Anwr Rawk simply you can use LEFT JOIN
select * from tbl_perspective as a
left join
tbl_objective as b
on b.idperspective=a.idperspective
Join to a subquery which identifies the first row for each idperspective group:
SELECT t1.*
FROM tbl_perspective t1
INNER JOIN
(
SELECT idperspective, MIN(idobjective) AS min_idobjective
FROM tbl_perspective
GROUP BY idperspective
) t2
ON t1.idperspective = t2.idperspective AND
t1.idobjective = t2.min_idobjective;
You need to use LEFT JOIN to include all results, like this : https://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins

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

MySql subselect havning where clause with value from main select

there's select which i'm trying to make work
SELECT DISTINCT a.client_key, client_name
FROM Bloggers AS a
LEFT JOIN BloggersPosts AS b
ON a.client_key = b.client_key
WHERE a.status = 1 AND
0 NOT IN (SELECT MIN(STATUS) FROM BloggersPosts AS c WHERE c.client_key=a.client_key)
For some reason 0 NOT IN (SELECT MIN(STATUS) FROM BloggersPosts AS c WHERE c.client_key=a.client_key) is not working, any ideas how to make it work?
EDIT: by not working i mean that if i delete the susbelect - my query gives result rows. But as soon as I add it - there is empty result. At the same time when i execute the subselect alone SELECT MIN(STATUS) FROM BloggersPosts - it returns 1, which means that putting it as subselect - should return results too, but it doesn't.
Thank you
I hope this will do your job. Trying different solution for your problem.
SELECT DISTINCT a.client_key, client_name
FROM Bloggers AS a
JOIN (SELECT client_key, MIN(STATUS) minstatus FROM BloggersPosts GROUP BY client_key) b
ON a.client_key = b.client_key AND minstatus <> 0
WHERE a.status = 1
Use JOIN instead, something like similar:
SELECT DISTINCT a.client_key, client_name, MIN(c.STATUS) blog_status
FROM Bloggers AS a
LEFT JOIN BloggersPosts AS b ON a.client_key = b.client_key
LEFT JOIN BloggersPosts AS c ON c.client_key = a.client_key
WHERE a.status = 1
HAVING blog_status <> 0

How to select from two tables based on a where clause

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

Mysql UNION by date

I get problem with query Mysql. I have 2 tables like at picture , and I want get result like at picture too...
how the query with UNION ? thanks .. sorry for my english
You can get this result with a LEFT JOIN:
SELECT
b.date_b as date_all,
a.quota_a,
b.quota_b
FROM
tabel_b b
LEFT JOIN
tabel_a a
ON
a.id_a = b.id_b
You've got to take the date info from tabel_b, because else you get a NULL value for id = 4.
Demo
If tabel_a can have more rows than tabel_b too, then you've got to use a FULL OUTER JOIN. Because MySQL hasn't got inbuild FULL OUTER JOIN you can emulate this by a UNION of a LEFT JOIN and a RIGHT JOIN. And as Strawberry has pointed out, maybe is the equality of the id columns the result of simplification. I add therefor the equality of the date columns to the join condition:
Please regard that you've got to use UNION, not UNION ALL to get only distinct rows.
SELECT
b.date_b as date_all,
a.quota_a,
b.quota_b
FROM
tabel_b b
LEFT JOIN
tabel_a a
ON
a.id_a = b.id_b
AND
a.date_a = b.date_b
UNION
SELECT
b.date_b as date_all,
a.quota_a,
b.quota_b
FROM
tabel_a a
LEFT JOIN
tabel_b b
ON
a.id_a = b.id_b
AND
a.date_a = b.date_b
Demo (Note: with the sample data gets this the same result as the first one).
if date is the parameter that "link" your tables [foreign key?] (from your answer title it seems), you could write the following query
SELECT date_a AS date_all, quota_a, quota_b
FROM tabel_B LEFT JOIN tabel_A ON date_a = date_b
ORDER BY date_all ASC
that way you'll obtain every record in tabel_b: if they have a corrispondence in tabel_a, either date_a and date_b will have a value, otherwise date_a will be null.
Please, pay attention: in that way you'll not obtain records that are present in tabel_a but not in tabel_b