There are three tables Users, Lists, Details. Here it is my table structure and data sample
userid username | list_id list_val user_id | detail_id list_id multipler
1 user1 | 1 500 1 | 1 1 3
2 user2 | 2 300 1 | 2 1 2
3 user3 | 3 600 1 | 3 2 1
4 100 2 4 2 1
5 3 4
SELECT
users.username,
SUM(lists.lists_var),
FROM
users
INNER JOIN lists ON users.userid = lists.user_id
GROUP BY
users.username
HAVING
(SELECT (exp(sum(log(COALESCE(details.multipler, 1))))) FROM details WHERE
details.list_id = lists.list_id) > 3
This query gives me result
user1 - 1400
But I want something IF multipler_total > 3 THEN SUM(list_val) so result must be:
user1 - 1100
try this:
SELECT
users.username,
SUM(lists.lists_var),
FROM
users
INNER JOIN lists ON users.userid = lists.user_id
INNER JOIN (SELECT list_id, SUM(multiplier) mult_total FROM details GROUP BY list_id) d
ON d.list_id = lists.list_id and d.mult_total > 3
GROUP BY
users.username
Related
My table setup is as follows
Shortlist
id (primary key)
property_id(foreign key)
user_id
1
100
2
2
101
2
3
103
4
4
100
1
5
100
3
6
101
1
Property
id (primary key)
account_id
100
1
101
2
102
3
103
4
I then have the following query
SELECT DISTINCT `s`.`user_id`, `s`.`property_id`, `p`.`id`, `p`.`account_number`, COUNT(`s`.`property_id`) as `shortlistCount`
FROM `shortlist` `s`
INNER JOIN `property` `p` ON s.`property_id` = p.`id`
WHERE s.`user_id` = "2"
GROUP BY s.`property_id`
LIMIT 10
I want to add a count field alias as shortlistCount that returns the total number of times each property_id field appears in the shortlist table. (or id field in the property table, whichever is easier in this context)
For example, the property_id of 100 appears 3 times in the shortlist table.
So the result should be
| user_id | property_id | id | account_number | shortlistCount |
|---------|-------------|-----|----------------|----------------|
| 2 | 100 | 100 | 1 | 3 |
| 2 | 101 | 101 | 2 | 2 |
However, shortlistCount currently always returns 1
How can I can update my shortlistCount alias to return the above result?
To count the property_ids you can use a correlated subquery:
SELECT s.user_id, s.property_id, p.id, p.account_number,
(select Count(*) from Shortlist s2 where s2.property_id = s.property_id) shortlistCount
FROM shortlist s
JOIN property p ON s.property_id = p.id
WHERE s.user_id = 2
LIMIT 10;
This query seemed pretty simple to start with.
#table - user
user_id - full_name - image
1 Jon-1 Jon-placeholder.png
2 Aax-2 Max-placeholder.png
3 Lie-3 Lie-placeholder.png
4 Man-4 Man-placeholder.png
5 Nik-5 Nik-placeholder.png
6 Led-6 Led-placeholder.png
7 Neo-7 Neo-placeholder.png
#table - user_contacts
id - user_id - contacts_id - created_date
1 1 2 2020-02-26 08:43:26
2 1 3 2020-02-26 08:43:26
3 1 5 2020-02-26 08:43:26
4 3 4 2020-02-26 08:43:26
5 3 5 2020-02-26 08:43:26
6 7 2 2020-02-26 08:43:26
What i am trying to a accomplish is retrieve all contacts_id from user_contacts where the user_id is not equal to user_id = 1 which user Jon-1 so this must looks like
#retrieved data
user_id - full_name - image
4 Man-4 Man-placeholder.png
6 Led-6 Led-placeholder.png
7 Neo-7 Neo-placeholder.png
user_ids 6,7,4 must be retrieved because user_id = 1 does not have those ids in the user_contact table under column contacts_id
First Query
SELECT u.user_id, u.full_name, u.image
FROM user u
NATURAL LEFT JOIN user_contacts cnt
WHERE cnt.contacts_id IS NULL
Second Query
SELECT user.user_id, user.full_name, user.image
FROM USER
WHERE user.user_id NOT IN (
SELECT user_contacts.contacts_id
FROM user_contacts
WHERE user_contacts.contacts_id IS NOT NULL)
Third Query
SELECT u.user_id, u.full_name, u.image
FROM user u
NATURAL LEFT JOIN user_contacts cnt
WHERE cnt.contacts_id IS NULL
dbfiddle
SELECT
user_id, full_name, image
FROM
user
WHERE NOT user_id in (SELECT contacts_id from user_contacts WHERE user_contacts.user_id=1)
AND user_id <> 1
WHERE NOT user_id in sub-query: The sub-query will get the contact for user=1.
AND user_id <> 1: this makes sure user 1 is not selected.
With NOT EXISTS:
select u.* from user u
where user_id <> 1
and not exists (
select 1 from user_contacts
where user_id = 1 and contacts_id = u.user_id
)
See the demo.
Results:
| user_id | full_name | image |
| ------- | --------- | ------------------- |
| 4 | Man-4 | Man-placeholder.png |
| 6 | Led-6 | Led-placeholder.png |
| 7 | Neo-7 | Neo-placeholder.png |
SELECT a.*
FROM user a
LEFT
JOIN
( SELECT uc.*
FROM user u
JOIN user_contacts uc
ON uc.user_id = u.user_id
WHERE u.full_name = 'Jon-1'
) b
ON b.contacts_id = a.user_id
WHERE a.full_name <> 'Jon-1'
AND b.id IS NULL;
I have users table
id | email | website
1 test#gmail.com google.com
2 blah#hotmail.com google.com
3 fred#outlook.com yahoo.com
4 jeff#gmail.com yahoo.com
And a groups table
id | group | user_id | created
1 group1 1 2018-06-06
2 group2 2 2018-06-06
3 group3 3 2018-09-09
4 group4 4 2018-06-06
I need to get a count of each website for each created day, so the above would give me 2 for google.com on 2018-06-06
Ie,
website | count | date
google.com 2 2018-06-06
yahoo.com 1 2018-06-06
yahoo.com 1 2018-09-09
I have the rows coming back grouped by date, but the count is all 1 for each row using the following query
select count(distinct users.website), groups.created
from user_groups as groups
join users on users.id = groups.user_id
group by groups.created
So i'm getting simply
count(distinct users.website) | created
1 2018-06-06
1 2018-06-06
1 2018-06-06
1 2018-09-09
Try below
DEMO
select website,created,count(website) as cnt
from groups t2 join users t1 on t1.id = t2.userid
group by website, created
Try
Select distinct(groups.created),count(distinct(users.website))
from user_groups as groups
join users on users.id = groups.user_id
group by groups.created
Should work.
Good Day,
I was creating a function where a user can share a post and other users may "LIKE or Comment" on it..
now on my POST table I have "id" and "source_id" + other details, to track people who like a post I created a table dedicated for like details. with columns: ID, USER_ID ( id of use who liked the post ), post_id
posts table:
id | source_id | caption
1 1 original post
2 1 share from id: 1 posts
3 3 new posts
4 4 new posts
likes table:
id | post_id | user_id
1 1 2
2 1 10
3 2 11
4 2 4
5 2 20
6 3 11
7 4 19
8 4 10
in order to count the number of like for a post, I do
SELECT a.id, a.source_id,b.num_likes
FROM posts AS a
LEFT JOIN (SELECT COUNT(user_id) AS
num_likes,post_id
FROM post_likes GROUP BY post_id ) AS b
ON b.post_id= a.id
WHERE b.num_likes != 0
result:
id | source_id | num_likes
1 1 2 <--- original post
2 1 3 <--- shared post who also got likes
3 3 1
4 4 2
what I would like to achieve is like these
id | source_id | num_likes
1 1 5
3 3 1
4 4 2
is this possible to achieve by just a query ? if so, could you please help me achieve this..
Thank you very much!
You can join the tables directly.
SELECT MIN(a.id), a.source_id, COUNT(b.num_likes) FROM posts AS a
LEFT JOIN post_likes AS b ON b.post_id= a.id
GROUP BY a.source_id
HAVING b.num_likes > 0
Might need to use a MIN for a.id since it is not aggregated. It would depend on your data and use case.
Use SUM function to get the total likes.
SELECT min(a.id) as id, a.source_id,sum(b.num_likes) as num_likes
FROM posts AS a
LEFT JOIN (SELECT count(1) AS
num_likes,post_id
FROM post_likes GROUP BY post_id ) AS b
ON b.post_id= a.id
WHERE b.num_likes != 0
GROUP BY a.source_id
Hi I was trying to create mysql statement that fit to my requirement.
2 tables as below
postTable
post_id | from_id
100 | 1
100 | 2
100 | 3
100 | 4
100 | 5
loveTable
post | uid
1 | 1
100 | 3
100 | 4
100 | 5
5 | 6
I want to select from_id from postTable where post_id=100 order by //uid that have post =100 in loveTable firstly.
Expecting result
from_id
3
4
5
1
2
Can you please advise me what is the right select statement?
select p.from_id
from postTable p left join lovetable o on p.from_id=o.uid
and o.post=100
where p.post_id=100
order by o.uid is not null desc,p.from_id
SQL FIDDLE HERE.
Try this:
SELECT from_id FROM postTable pt
LEFT JOIN loveTable lt
ON pt.from_id = lt.uid
WHERE pt.post_id = 100
ORDER BY lt.post desc
See this SQLFiddle