MySQL select with min - mysql

I need to select fields message and username from table list where list_id=1 (it can be 2 or 5 etc) with minimal number value (min(number)). How can i do it?
I tried it:
SELECT `message`,`username` FROM `list` WHERE `list_id`=2 AND min(`number`)
But it not work.

Try so
SELECT `message`,`username`
FROM `list`
WHERE `list_id` = 2
ORDER BY `number` ASC
LIMIT 1

SELECT
a.*
FROM list
INNER JOIN (
SELECT
`message`,
`username`,
MIN(`number`)
FROM `list`
WHERE `list_id`=2
) as a on a.id = list.id

Related

Query with aggregate, subquery and group by not working

Can you help me, please? I spent about 2 hours to understand what is wrong, but still don't.
SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name
'id'
select count(*) as aggregate
from (
select `cities`.*,
`cities`.`id` as `id`,
`cities`.`country_id` as `country_id`,
`cities`.`name` as `name`,
`cities`.`alias` as `alias`,
`cities`.`active_frontend` as `active_frontend`
from `cities`
where (
cities.alias in (
select `alias`
from `cities`
group by `alias`
having COUNT(`alias`) > 1
)
)
) count_row_table
Don't ask me what the hell is going on please. Biggest part of this query is generated by Laravel.
If I delete this part:
where
(cities.alias IN (SELECT alias FROM cities GROUP BY alias HAVING
COUNT(alias) > 1))
It will work. But I need this part af.
The issue is with cities.*.
But you can simplify your query to:
select sum(cnt) as cnt
from (
select COUNT(alias) as cnt
from cities
group by alias
having COUNT(alias) > 1
) t
and avoid re-reading your table because in the end, all your need is total number of rows for which alias has more than one row.
You don't need to materialize a subquery for this. You can do:
select count(*)
from cities c
where exists (select 1 from cities c2 where c2.alias = c.alias and c2.id <> c.id);
With an index on cities(alias, id), this should have better performance.

insert into table(var1) select from itself

I want to insert into the last column (number of people in that room) and
I want to use
insert into table(n_people_in_room)
select count(people_id)
from table
group by room
This is obvious wrong because i need to join the table with itself on people_id but i didn't. What is the right code?
Here's one way to do it, using an inline view to get the N_People_In_Room totals:
I'd do it as a SELECT first:
SELECT t.peopleid
, t.room
, t.n_people_in_room AS `old_npir`
, s.n_people_in_room AS `new_npir`
FROM mytable t
JOIN ( SELECT c.room
, COUNT(1) AS n_people_in_room
FROM mytable c
GROUP BY c.room
) s
ON s.room = t.room
Convert that into an UPDATE by repacing SELECT ... FROM with UPDATE, and adding a SET clause...
UPDATE mytable t
JOIN ( SELECT c.room
, COUNT(1) AS n_people_in_room
FROM mytable c
GROUP BY c.room
) s
ON s.room = t.room
SET t.n_people_in_room = s.n_people_in_room

MySQL Select from UNION performance issue (kills database)

I have a little problem regarding MySQL
I'm trying to make a UNION of two tables like so:
SELECT `user_id`, `post_id`, `requested_on`
FROM `a`
WHERE `status` != 'cancelled'
UNION
SELECT `user_id`, `post_id`, `time` as requested_on
FROM `b`
WHERE `type` = 'ADD'
This query is executed in Showing rows 0 - 29 (36684 total, Query took 0.0147 sec)
but when I do
SELECT * FROM (
SELECT `user_id`, `post_id`, `requested_on`
FROM `a`
WHERE `status` != 'cancelled'
UNION
SELECT `user_id`, `post_id`, `time` as requested_on
FROM `b`
WHERE `type` = 'ADD'
) tbl1
MySQL dies.
The reason why I want to do this is to GROUP BY user_id, post_id
Any ideas why this happens / any workarounds?
later-edit:
This is the SQL Fiddle:
http://sqlfiddle.com/#!2/c7f82d/2
The final query is there, which executes in:
Record Count: 10; Execution Time: 574ms
574ms for 10 records in my point of view is gigantic.
I found what the problem was from.
It was the fact that I was running the queries in PHPMyAdmin and when I did a SELECT UNION SELECT everything was good but when I did
SELECT * FROM (SELECT UNION SELECT)
the pagination system from PHPMyAdmin failed, and PHPMyAdmin was trying to output to my browser a over 30k rows table, that's why the SQL Request hang. :(
It is not clear what the question:
SELECT * FROM (
SELECT user_id, post_id, requested_on
FROM a
WHERE status != cancelled
UNION
SELECT user_id, post_id, time as requested_on
FROM b
WHERE type = ADD
) tbl1 GROUP BY user_id, post_id
means. Assume you have:
A, x, t1
A, x, t2
would you like the row with t1 or t2? If that does not matter lets apply an aggregate function such as MIN:
SELECT user_id, post_id, MIN(requested_on) FROM (
SELECT user_id, post_id, requested_on
FROM a
WHERE status <> cancelled
UNION
SELECT user_id, post_id, time as requested_on
FROM b
WHERE type = ADD
) tbl1
GROUP BY user_id, post_id
MySQL usually doesn't handle derived tables like this very well, is there any other predicate that you can apply to the parts in the union?

Update if count is greater than 5 in MySQL

I'm trying to create a query that'll update table_1 where column id_owner has more than 5 rows with the same owner id, it needs to set column "active" to 3 on all rows those users have.
I've tried several different methods and turned up empty with each. Any ideas?
Use this UPDATE query with JOIN to achieve this:
UPDATE table1 t1
JOIN
(
SELECT id_owner
FROM table1
GROUP BY id_owner
HAVING COUNT(*) > 5
) t2
ON t1.id_owner = t2.id_owner
SET t1.active = 3;
See this sample SQLFiddle
You may try this:-
update table_1
set active = 3
where owner_id in
(
select * from
(
select owner_id
from table_1
group by owner_id
having count(*) > 5
) a
)
update table_1
set active = 3
where owner_id in
(
select * from
(
select owner_id
from table_1
group by owner_id
having count(*) > 5
) x
)
SQLFiddle demo

MySQL - Using COUNT(*) in the WHERE Clause?

Ok so I want to find all the rows with the same value in. (Or at least a pair)
I.E.
James| 19.193.283.19
John| 20.134.232.344
Jack| 19.193.283.19
Jonny| 19.193.283.19
I would want it to return rows James, Jack and Jonny -as more than one row has the IP '19.193.283.19' in it.
I tried doing what the other similar question answered:
select *
from `Zombie`
group by `Ip`
having count(*) > 1
order by `Ip` desc
But it just returned 1 row with a pair or more of the similar 'Ip' I want every row.
How would I modify the SQL so it returns all indisinct rows?
Thanks alot.
You could use an exists subquery to find all rows that have a matching row with the same Ip:
select *
from YourTable as yt1
where exists
(
select *
from YourTable as yt2
where yt1.name <> yt2.name
and yt1.Ip = yt2.Ip
)
Sorting by the number of rows with the same Ip can be done with a self-join, like:
select yt1.name
, yt1.Ip
from YourTable as yt1
join YourTable as yt2
on yt1.name <> yt2.name
and yt1.Ip = yt2.Ip
group by
yt1.name
, yt1.Ip
order by
count(yt2.name) desc
Another way would be to join your table with the subquery you already have used (to find ips existing in more than one row):
SELECT t.name
, t.Ip
FROM
YourTable AS t
JOIN
( SELECT Ip
, COUNT(*) AS cnt
FROM YourTable
GROUP BY Ip
HAVING COUNT(*) > 1
) AS td
ON td.Ip = t.Ip
ORDER BY
td.cnt DESC
, t.Ip
, t.name