I have a table with the following columns:
Categorie
Rubriek
Adv_nr
For each rubriek it is possible that there are 100 or more adv_nr's.
I want to select max 5 rows from each rubriek, is this possible in one query?
select * from table as t1
where (select count(*) from table as t2
where t1.rubriek = t2.rubriek and t2.adv_nr > t1.adv_nr) < 5
order by rubriek,adv_nr desc
select * from *ad_table*
where category_id IN (
select *category_table*.id
from *category_table*, *group_table*
where *category_table*.id = *group_table*.category_id)
LIMIT 5;
Related
The following is my db table:
id category_id name
--------------------
1 4 A
2 5 B
3 6 C
I have four simple select queries which pull 15 rows by random from specific categories:
select * from table where category_id = 4 order by rand() limit 15;
select * from table where category_id = 5 order by rand() limit 15;
select * from table where category_id = 6 order by rand() limit 15;
select * from table where category_id = 7 order by rand() limit 15;
I want to combine them into a single query rather than four separate queries. I've tried using the UNION operator but it wasn't pulling 15 rows EQUALLY from each category:
(
select * from table where category_id = 4
union
select * from table where category_id = 5
union
select * from table where category_id = 6
union
select * from table where category_id = 7
) order by rand() limit 60;
How can I achieve this? Or, do I have to run separate queries?
I've tagged Laravel because I'm using Laravel as the backend and maybe Eloquent has a smarter way to achieve this.
Have you tried this one?:
(select * from table where category_id = 4 ORDER BY rand() LIMIT 15)
union all
(select * from table where category_id = 5 ORDER BY rand() LIMIT 15)
union all
(select * from table where category_id = 6 ORDER BY rand() LIMIT 15)
union all
(select * from table where category_id = 7 ORDER BY rand() LIMIT 15)
you could use CTE and ROW_NUMBER() as such
WITH CTE AS (
SELECT id,
category_id,
ROW_NUMBER() OVER(PARTITION BY category_id
ORDER BY RAND() DESC) AS rank
FROM table)
SELECT *
FROM CTE
WHERE rank <= 15 AND category_id IN (4,5,6,7)```
This is output
but i want
in this table tb_user_connection i get 4 row but i want only 3 so, if i get connection in 1 and 2 so not get repeated column.
i try below query and this is codeigniter project.
my query:
SELECT * FROM `tb_user_connection` WHERE `connection_type` = 'a' AND (user_id = '1' OR connection_id='1')
You can do this:
select *
from your_table
where user_id <= connection_id
union all
select *
from your_table t
where user_id > connection_id
and not exists (
select 1
from your_table t1
where t1.user_id = t.connection_id
and t1.connection_id = t.user_id
) t1;
I have a query that gets rows ordered by column a and limited to 100. Then I want to run a query on that result that gets rows ordered by column b and limited by 50.
How can I do this?
Do the first order by/limit in a derived table. Then do the second order by/limit on the derived table's result:
select * from
(
select * from tablename
order by a
limit 100
) dt
order by b
limit 50
You should use select from select statement:
select a, b
from (
select a, b
from table1
order by a
limit 100
)
order by b
limit 50
Here's a query:
SELECT *
FROM table
WHERE id = 1
OR id = 100
OR id = 50
Note that I provided the ids in this order: 1,100,50.
I want the rows to come back in that order: 1,100,50.
Currently, i comes back 1,50,100 - basically in ascending order. Assume the rows in the table were inserted in ascending order also.
Use the MySQL specific FIND_IN_SET function:
SELECT t.*
FROM table t
WHERE t.id IN (1, 100, 50)
ORDER BY FIND_IN_SET(CAST(t.id AS VARCHAR(8)), '1,100,50')
Another way to approach this would put the list in a subquery:
select table.*
from table join
(select 1 as id, 1 as ordering union all
select 100 as id, 2 as ordering union all
select 50 as id, 3 as ordering
) list
on table.id = list.id
order by list.ordering
You can just do this with ORDER BY:
ORDER BY
id = 1 DESC, id = 100 DESC, id = 50 DESC
0 is before 1 in ORDER BY.
Try this
SELECT *
FROM new
WHERE ID =1
OR ID =100
OR ID =50
ORDER BY ID=1 DESC,ID=100 DESC,ID=50 DESC ;
http://www.sqlfiddle.com/#!2/796e2/5
... WHERE id IN (x,y,x) ORDER BY FIELD (id,x,y,z)
I need to a MySQL query that returns the results of a table where the elements of a specific column are identical in at least 'n' records.
example pseudocode:
SELECT * FROM Table WHERE col has at least 3 identical
select * from table where col in (
select col from table group by col having count(*) > 3
)
SELECT col, COUNT(*) AS total FROM tbl GROUP BY col HAVING total >= 3
select * from table a
inner join (
select col,n=count(*)
from table
group by col
having count(*) >= 3 ) as b
on (a.col=b.col)
mysql allows you to write something like the following:
select *
from table
group by col
having count(*) >= 3
other dbmss force you to specify all columns explicitly and use MAX or MIN on them
SELECT DISTINCT * FROM TableName t WHERE 2 < (SELECT COUNT(*) FROM TableName p WHERE p.col = t.col)