How can I count when I get my data from two column? - mysql

I have got 2 tables in SQL.
hfm_files => id, user_id, etc...
downloaded => id, hfm_file_id, etc...
And I know my user id. How can I count how many download was for one file with simple SQL?
I tried to make some query, but I cant make it:
UPDATE user u
SET u.affilite_pont = (SELECT COUNT(*) FROM downloaded vm WHERE

Try to get count by joining both tables:
SELECT COUNT(d.ID)
FROM downloaded d
JOIN hfm_files h
ON d.hfm_file_id = h.id
WHERE h.user_id = 1;
So you update query should be:
UPDATE user u
SET u.affilite_pont = (
SELECT COUNT(d.ID)
FROM downloaded d
JOIN hfm_files h
ON d.hfm_file_id = h.id
WHERE h.user_id = 1;
)
WHERE ...

SELECT COUNT(t1.id) FROM
downloaded t1, hfm_files t2
WHERE t2.id = t1.hfm_file_id
AND t2.user_id = <user_id>

Related

I have a table like below I want the latest entry to be displayed

The query which I am using now is below:
select ur.uid
, ua.user_activity_min_budget
, ua.user_activity_max_budget
, ua.user_activity_bedroom
, ptm.property_type_description
, cm.city_name
, lm.locality_name
, ua.user_activity_datetime
from user_registration ur
join ksl_user_activity ua
on ua.registered_user_uid = ur.uid
and ua.user_activity_uid = ( select max(ua0.user_activity_uid) from ksl_user_activity ua0)
join ksl_locality_master lm
on lm.locality_uid = ua.user_activity_area
join ksl_city_master cm
on cm.city_uid = lm.city_uid
join ksl_property_type_master ptm
on ptm. property_type_uid = ua.user_activity_property_type
where date(ua.user_activity_datet±me) >= '20l7-07-24'
and (lm.city_uid = 1 or lm.city_uid=2)
order
by ur.uid
The raw output s as this image shows:
The data is what I get now but I want the latest entry for uid 3,15,33
The reason why I have done the below is and ua.user_activity_uid=(select max(ua0.user_activity_uid) from user_activity ua0).
ksl_user_activity table has a primary key user_activity_id which has the maximum value for the latest entry but I am not getting any data when I include this in my query.
I also tried and ua.user_activity_uid=(select ua0.user_activity_uid from user_activity ua0 order by ua0.user_activity_uid desc limit 1)
This is also not working.
use max() function and sub-query
select t1.uid from user_activity t1
inner join
(select uid,max(user_activity_datetime) as user_activity_datetime from user_activity group by uid
) as t2 on
t1.user_activity_datetime=t2.user_activity_datetime
and t1.uid=t2.uid

Multiple Inner Join Concern

I'm trying to get data at monthly level.
SELECT
c.Calendar_Month_Name, COUNT(*)
FROM
db1 AS c
INNER JOIN
(SELECT DISTINCT
a.tel_num, b.postpaid_tel_num
FROM
db2 AS a
INNER JOIN db3 AS b ON a.tel_num = b.tel_num
WHERE
a.hs_manufacturer = 'Samsung'
AND b.postpaid_tel_num = 1) d ON c.Dim_Calendar_Dt = d.REPORT_DT
WHERE
c.Calendar_Year_Num = 2018
GROUP BY c.Calendar_Month_Name;
REPORT_DT is present in db2 but still I get an error that says REPORT_DT does not exist
If I change the position of paratheses as follows I get an error that says, something is expected between 'REPORT_DT' and the 'where' keyword.
SELECT
c.Calendar_Month_Name, COUNT(*)
FROM
(db1 AS c
INNER JOIN (SELECT DISTINCT
a.tel_num, b.postpaid_tel_num
FROM
db2 AS a
INNER JOIN db3 AS b ON a.tel_num = b.tel_num
WHERE
a.hs_manufacturer = 'Samsung'
AND b.postpaid_tel_num = 1) d ON c.Dim_Calendar_Dt = d.REPORT_DT
WHERE
c.Calendar_Year_Num = 2018)
GROUP BY c.Calendar_Month_Name;
In the first version, it looks like you need to add REPORT_DT to the select clause of your subquery d
FWIW, I think a formatted query should look something like this:
SELECT c.Calendar_Month_Name
, COUNT(*)
FROM db1 c
JOIN
( SELECT DISTINCT a.tel_num
, b.postpaid_tel_num
FROM db2 a
JOIN db3 b
ON a.tel_num = b.tel_num
WHERE a.hs_manufacturer = 'Samsung'
AND b.postpaid_tel_num=1
) d
ON c.Dim_Calendar_Dt = d.REPORT_DT
WHERE c.Calendar_Year_Num = 2018
GROUP
BY c.Calendar_Month_Name

How to update multiple columns from different table of MariaDB

I am using MariaDB. I am trying to update two columns from SELECT different table.
UPDATE User U
SET
U.UserPoint = (
SELECT ((SELECT COUNT(*)
FROM CARD_COMM R
WHERE R.Card_ID = C.Card_ID) * 3
+
(SELECT COUNT(*)
FROM SECTION_CARD_LIKE L
WHERE L.Card_ID = C.Card_ID) * 1) as userPoint
FROM CARD C WHERE C.userid = U.userid ORDER BY userPoint DESC limit 1 )
this works
UPDATE User U
SET
(U.UserPoint, U.Card) = (
SELECT ((SELECT COUNT(*)
FROM CARD_COMM R
WHERE R.Card_ID = C.Card_ID) * 3
+
(SELECT COUNT(*)
FROM SECTION_CARD_LIKE L
WHERE L.Card_ID = C.Card_ID) * 1) as userPoint,
C.Card_ID as card
FROM CARD C WHERE C.userid = U.userid ORDER BY userPoint DESC limit 1 )
but this dose not....
How do I do this??
please help me...
Use a multi-table update, something like
UPDATE User
JOIN ( SELECT userid, up_value, card_value ... ) AS x
ON x.userid = User.userid
SET User.UserPoint = x.up_value,
User.Card = x.card_value;
(With suitable expressions/subqueries/etc for up_value & card_value)
You seem to be updating all rows in User??

(My)SQL JOIN - get teams with exactly specified members

Assume tables
team: id, title
team_user: id_team, id_user
I'd like to select teams with just and only specified members. In this example I want team(s) where the only users are those with id 1 and 5, noone else. I came up with this SQL, but it seems to be a little overkill for such simple task.
SELECT team.*, COUNT(`team_user`.id_user) AS cnt
FROM `team`
JOIN `team_user` user0 ON `user0`.id_team = `team`.id AND `user0`.id_user = 1
JOIN `team_user` user1 ON `user1`.id_team = `team`.id AND `user1`.id_user = 5
JOIN `team_user` ON `team_user`.id_team = `team`.id
GROUP BY `team`.id
HAVING cnt = 2
EDIT: Thank you all for your help. If you want to actually try your ideas, you can use example database structure and data found here: http://down.lipe.cz/team_members.sql
How about
SELECT *
FROM team t
JOIN team_user tu ON (tu.id_team = t.id)
GROUP BY t.id
HAVING (SUM(tu.id_user IN (1,5)) = 2) AND (SUM(tu.id_user NOT IN (1,5)) = 0)
I'm assuming a unique index on team_user(id_team, id_user).
You can use
SELECT
DISTINCT id,
COUNT(tu.id_user) as cnt
FROM
team t
JOIN team_user tu ON ( tu.id_team = t.id )
GROUP BY
t.id
HAVING
count(tu.user_id) = count( CASE WHEN tu.user_id = 1 or tu.user_id = 5 THEN 1 ELSE 0 END )
AND cnt = 2
Not sure why you'd need the cnt = 2 condition, the query would get only those teams where all of users having the ID of either 1 or 5
Try This
SELECT team.*, COUNT(`team_user`.id_user) AS cnt FROM `team`
JOIN `team_user` ON `team_user`.id_team = `team`.id
where `team_user`.id_user IN (1,5)
GROUP BY `team`.id
HAVING cnt = 2

How to optimize this mysql query? Runs slow

I'm using a database that, imho, wasn't designed well, but maybe it's just me not understanding it. Anyways, I have a query that pulls the correct information, but it is really slowing down my php script. I was hoping someone could take a look at this and let me know if nesting queries to this depth is bad, and whether or not there is a way to simplify the query from the relationships depicted in the sql statement below.
SELECT name
FROM groups
WHERE id = (SELECT DISTINCT immediateparentid
FROM cachedgroupmembers
WHERE groupid = (SELECT g.id AS AdminCc
FROM Tickets t, groups g
WHERE t.Id = 124 AND t.id = g.instance AND g.type = 'AdminCc')
AND immediateparentid <> (SELECT g.id AS AdminCc
FROM Tickets t, groups g
WHERE t.Id = 124 AND t.id = g.instance AND g.type = 'AdminCc'))
Please help
Update:
Here is the output from using Explain
You may need to right click and select "View Image" for the text to be clear.
From what I can tell, you can eliminate one sub-select.
SELECT name
FROM groups
WHERE id = (
SELECT DISTINCT immediateparentid
FROM cachedgroupmembers
WHERE groupid = (
SELECT g.id
FROM Tickets t, groups g
WHERE t.Id = 124 AND t.id = g.instance AND g.type = 'AdminCc'
) AND immediateparentid != groupid
)
I'm much more used to PL/SQL on Oracle but I'll give it a try.
Get rid of aliases, you don't need them here.
Make sure columns used in the where clause are indexed (t.Id and g.type).
Don't know if MySQL indexes foreign keys by default but worth the check.
You can shorten your SQL code like that:
SELECT name
FROM groups
WHERE id = (
SELECT DISTINCT immediateparentid
FROM cachedgroupmembers
WHERE groupid = (
SELECT g.id
FROM Tickets t, groups g
WHERE t.Id = 124 AND t.id = g.instance AND g.type = 'AdminCc'
) AND immediateparentid != groupid
)
or:
SELECT name
FROM groups
WHERE id = (
SELECT DISTINCT immediateparentid
FROM cachedgroupmembers
WHERE groupid = (
SELECT g.id
FROM Tickets t inner join groups g on t.id = g.instance
WHERE t.Id = 124 AND g.type = 'AdminCc'
) AND immediateparentid != groupid
)
if your tickets table is big you may consider a temp table instead of querying it twice