I want to get all the user_id that are having different user_ip_address on user_device table using a MySQL query.
Table: user_device
id user_id user_ip_address
-----------------------------------
1 1 1.1.1.1
2 1 1.1.1.1
3 2 2.2.2.2
4 3 3.3.3.3
5 1 10.10.10.10
6 4 4.4.4.4
7 2 20.20.20.20
8 3 3.3.3.3
In this data, user_id 1 and user_id 2 have different user_ip_address.
It should output 1 and 2
I tried:
SELECT user_ip_address AS c
FROM user_device WHERE user_ip_address IN (SELECT user_id
FROM user_device group by user_id having user_ip_address!=c)
Use:
select user_id
from user_device
group by user_id
having count(distinct user_ip_address) >1;
Result:
user_id
1
2
Demo
Count the number of different addresses each user has:
SELECT user_id
FROM user_device
GROUP BY user_id
HAVING COUNT(DISTINCT user_ip_address) > 1
untested:
select user_id
from (
select user_id
from user_device
group by user_id, user_ip_address
)
group by user_id
having count(*) > 1
should give you in theory user_id = 1 and 2
Related
I'm working on a small search engine project and I need some help with a SQL query. My table looks like this (example):
user_id | group_id
---------------------------
1 | 2
2 | 2
2 | 3
3 | 2
I would like to look for : user_id who has group_id = 2 and 3
So a possible result would be:
user_id | group_id
---------------------------
2 | 2
2 | 3
How should the select query look like?
I will suppose your table name is users and we will select only two columns user_id and group_id from the users table
SELECT user_id, group_id FROM users WHERE group_id IN (2, 3);
You want something like this:
SELECT *
FROM your_table
WHERE user_id IN (
SELECT DISTINCT user_id
FROM your_table
WHERE group_id IN (2,3)
GROUP BY user_id
HAVING COUNT(user_id) > 1 )
Check demo.
EDIT. I have modified my answer to below to be more accurate as pointed out by #ysth:
SELECT DISTINCT user_id,
group_id
FROM usergroups
WHERE user_id IN (
SELECT DISTINCT user_id
FROM (SELECT DISTINCT user_id,
group_id
FROM usergroups ) g
WHERE group_id IN (2,3)
GROUP BY user_id
HAVING COUNT(user_id) > 1 )
Check demo.
I have two tables:
goods
id parent_id name
1 null main_part
2 1 add_part1
3 1 add_part2
4 1 add_part3
orders
id good_id count
1 1 5
I want to get a view as :
orders_view
id good_id count
1 1 5
2 2 5
3 3 5
4 4 5
How to do in MySQL?
If your relation is a single level you could try joining the subquery for the union with the orders
select t1.id, t1.id good_id, o.count
from (
select id id_master, id
from goods
where parent_id is null
union all
select parent_id, id
where parent_id is not null
) t
inner join orders_view o on o.good_id = t1.id_master
Hi I have two tables one is user and another one is user_details
The user table is
id name
---------
1 John
2 Tom
3 Amy
4 Kat
The user_details table is
id user_id money created_on
------------------------------------
1 1 2000 2016-12-01
2 1 5000 2016-12-02
3 1 6000 2016-12-03
4 2 2050 2016-12-01
5 2 5020 2016-12-02
6 3 6000 2016-12-04
And I need result like
name user_id money created_on
------------------------------------
John 1 6000 2016-12-03
Tom 2 5020 2016-12-02
Amy 3 6000 2016-12-03
Kat 4 NULL NULL
My question is I need records where the recent created_on of user_details table, for this I wrote following query but I am getting all the results could any one tell me what wrong in my query
SELECT * FROM `user` as `u` LEFT JOIN ( SELECT user_id,money,created_on FROM `user_details` WHERE created_on IN (SELECT MAX(created_on) FROM user_details group by user_id )) `userdata` ON `u`.`id` = `userdata`.`user_id`;
In your inner subquery, you just group by the user_id, but don't filter by the user_id of the user you are currently selecting in your other query.
In other words: You don't even need to group by user_id, just select the MAX(created_on) of the given user by using an alias in the outer subquery like so:
SELECT * FROM `user` as `u` LEFT JOIN
( SELECT user_id,money,created_on FROM `user_details` x WHERE created_on IN
(SELECT MAX(created_on) FROM user_details
WHERE x.user_id = user_details.user_id))
`userdata` ON `u`.`id` = `userdata`.`user_id`;
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
I have this table structure
| id | classid | contextid |
----------------------------
1 2 2
2 3 1
3 2 1
4 3 1
5 1 2
5 1 4
How to fetch count of every classid from DB table in Mysql I need a select query for it?
Count of every classid... do you mean something like this:
SELECT classid, COUNT(*) as cnt FROM tbl_name GROUP BY classid
SELECT classid, COUNT(*) FROM table1 GROUP BY classid;
SELECT classid, COUNT(classid) classid_count FROM table_name GROUP BY classid;