Join query with limit results - mysql

ID Name
1 A
2 B
3 C
4 D
5 E
6 F
7 G
8 H
9 I
10 J
And other table is
ID Image Date
1 aa.jpg 5/17/2016
1 bb.jpg 5/20/2016
1 aa1.jpg 5/15/2016
2 1.jpg 5/17/2016
3 2.jpg 5/17/2016
3 3.jpg 5/20/2016
4 x.jpg 5/17/2016
8 tt.jpg 5/17/2016
9 ww.jpg 5/21/2016
10 21.jpg 5/17/2016
5 67.jpg 5/17/2016
6 3d.jpg 5/17/2016
7 w3.jpg 4/17/2016
7 y78.jpg 5/17/2016
I have written below query to get this result.
select t1.id,t1.name,t2.image,t2.date
from emp t1
join images t2 ON t1.id = t2.id
result:
ID Name Images Date
2 B 1.jpg 5/17/2016
1 A bb.jpg 5/20/2016
1 A aa1.jpg 5/15/2016
1 A aa.jpg 5/17/2016
.
.
.
Here is my actual problem:
I have to add limit functionality like limit 0,100. in my limit 0,3
So my expectation is if any id coming with query so all id should be come.

I guess your requirement is to choose a set of ID values that yields approximately 100 rows in your result set.
LIMIT makes very little sense without ORDER BY; you're just choosing an unpredictable subset of the result set. So, I will guess you want to ORDER BY ID.
First, you need to figure out which ID values are in your set. That's relatively simple. (I'm using LIMIT 0,5).
SELECT DISTINCT id
FROM (
select t1.id
from emp t1
join images t2 ON t1.id = t2.id
order by t1.id
limit 0,5
) a
Then, use that as a subquery to limit your query. Here's a SqlFiddle demonstrating this (http://sqlfiddle.com/#!9/44430d/4/0).
select t1.id
from emp t1
join images t2 ON t1.id = t2.id
where t1.id IN (
SELECT DISTINCT id
FROM (
select t1.id
from emp t1
join images t2 ON t1.id = t2.id
order by t1.id
limit 0,5
) a
)
order by t1.id
This query is likely to yield more rows than your LIMIT, of course: it augments its result set with extra images as necessary. The SqlFiddle example yields six rows rather than five (http://sqlfiddle.com/#!9/44430d/4/0)

Related

How can I get the result from mysql after group and order

I have the following tables
t1
id
stage
1
1,2,3
2
2,3,4
t2
id
t_id
stage_id
1
1
2
2
1
1
3
1
3
4
2
2
5
2
4
6
2
3
I hope the result can first order by t2.t_id and then order by the value of t1.stage
like the following result
t_id
stage_id
1
1
1
2
1
3
2
2
2
3
2
4
I have the following sql ,but it do not work.So what should I do?
SELECT
t2.t_id,
t2.stage_id
FROM
t2
LEFT JOIN t1 ON t1.id = t2.t_id
GROUP BY
t2.t_id,
t2.stage_id
ORDER BY
t2.t_id,
field(t2.stage_id, t1.stage)
FIELD() requires each value to sort by to be a separate argument.
FIND_IN_SET() returns the index in a comma-separated string.
SELECT
t2.t_id,
t2.stage_id
FROM
t2
LEFT JOIN t1 ON t1.id = t2.t_id
GROUP BY
t2.t_id,
t2.stage_id
ORDER BY
t2.t_id,
find_in_set(t2.stage_id, t1.stage)
Note that this only corrects the ordering criteria in your SQL. This won't necessarily return the first row in each group by that ordering (and will get an error if the ONLY_FULL_GROUP_BY SQL mode is enabled). See SQL Selecting from two Tables with inner join and limit for the correct way to do that.

How to combine `MAX` and `JOIN` to get correct output?

I have the select output :
SELECT
t1.NO_PROJECT,
t1.ID_ACTIVITY,
t2.NO,
t2.NAMA_ACTIVITY,
t2.LEVEL_ACTIVITY
FROM t1
INNER JOIN t2
ON t1.ID_ACTIVITY = t2.NO
ORDER BY LEVEL_ACTIVITY DESC
LIMIT 1
The result of this select query is:
NO_PROJECT ID_ACTIVITY NO NAMA_ACTIVITY LEVEL_ACTIVITY
PRJ-2017-544 2 2 03 DRTU 3
PRJ-2017-544 6 6 09 P.HARGA 9
PRJ-2017-544 7 7 08 HPS 8
I use ORDER BY LEVEL_ACTIVITY DESC and LIMIT 1 to get my desired output. But, how do I do it if I use MAX(LEVEL_ACTIVITY)?
the output should be (when I use ORDER BY LEVEL_ACTIVITY DESC and LIMIT 1) :
NO_PROJECT ID_ACTIVITY NO NAMA_ACTIVITY LEVEL_ACTIVITY
PRJ-2017-544 6 6 09 P.HARGA 9
I'd like to know how do I get the same output when using MAX(LEVEL_ACTIVITY)
You can join with a subquery to get MAX(level_activity).
SELECT
t1.NO_PROJECT,
t1.ID_ACTIVITY,
t2.NO,
t2.NAMA_ACTIVITY,
t2.LEVEL_ACTIVITY
FROM t1
INNER JOIN t2
ON t1.ID_ACTIVITY = t2.NO
INNER JOIN (SELECT MAX(LEVEL_ACTIVITY) AS MAX_LA FROM t2) AS t3
ON t2.LEVEL_ACTIVITY = t3.MAX_LA
If you do this, and there are multiple rows in t2 with the highest LEVEL_ACTIVITY, they'll all be in the results. Or if there are multiple rows in t1 that join with the highest level activity, they'll all be in the results.
If you only want 1 result, your original method should work. You can't get 3 rows as you show when you use LIMIT 1.
SELECT
t1.NO_PROJECT,
t1.ID_ACTIVITY,
t2.NO,
t2.NAMA_ACTIVITY,
t2.LEVEL_ACTIVITY,
(SELECT MAX(LEVEL_ACTIVITY) AS MAX FROM t2)
FROM t1
INNER JOIN t2 ON t1.ID_ACTIVITY = t2.NO
ORDER BY LEVEL_ACTIVITY DESC
LIMIT 1`

SQL select rows with number in sequence

I have 1:N table where every entity may have asigned multiple numbers.
ID Number
1 10
1 13
1 11
1 12
1 16
2 11
2 12
2 13
2 10
Now,I want all IDs which have for example 3 numbers in ascending sequence. I do not specify which numbers I want, I just want the SQL to return me all possible combinations it can find but the numbers has to be in ascending sequence and the sequence must contain exactly 3 numbers. The numbers are allways integers of any value. The numbers in result have to be next to each other (12,13,16)is not valid result.
For 3 numbers in this example it would be :
ID 1 : (10,11,12),(11,12,13)
ID 2 : (11,12,13),(10,11,13)
For 2 numbers in this example it would be:
ID 1 : (10,11),(11,12),(12,13)
ID 2 : (11,12)(12,13)
Is this possible in SQL select? Thanx
A solution whats comes close to your expected output.
Involves using self inner joins incombination with CONCAT_WS, GROUP_CONCAT..
For group of three you use this query
Query
SET SESSION group_concat_max_len = ##max_allowed_packet
SELECT
records.ID
, GROUP_CONCAT(CONCAT('(', records.number, ')'))
FROM (
SELECT
DISTINCT
table11.ID
, CONCAT_WS(
','
, table11.Number
, table12.Number
, table13.Number
) AS number
FROM
Table1 AS table11
INNER JOIN
Table1 AS table12
ON
table11.Number + 1 = table12.Number
INNER JOIN
Table1 table13
ON
table12.Number + 1 = table13.Number
ORDER BY
table11.ID ASC
, table11.Number ASC
) AS records
GROUP BY
records.ID
Result
| ID | GROUP_CONCAT(CONCAT('(', records.number, ')')) |
|----|------------------------------------------------|
| 1 | (11,12,13),(10,11,12) |
| 2 | (11,12,13),(10,11,12) |
see demo http://sqlfiddle.com/#!9/c5dfce/39
Simply use join. This produces a result set with each examples of sequential numbers on a different row:
select id, t1.number, t2.number, t3.number
from t t1 join
t t2
on t2.id = t1.id and t2.number = t1.number + 1 join
t t3
on t3.id = t2.id and t3.number = t2.number + 1;
If you really wanted a list, you would simply do:
select id,
group_concat('(', t1.number, ',', t2.number, ',', t3.number, ')') as groups
from t t1 join
t t2
on t2.id = t1.id and t2.number = t1.number + 1 join
t t3
on t3.id = t2.id and t3.number = t2.number + 1
group by t1.id;

Joining two tables on interval basis

In SQL, suppose that I have table A
ID
--
1
3
5
and table B
ID2
---
1
2
3
4
5
6
To get the result similar to:
ID | ID2
----------
1 | 1
1 | 2
3 | 3
3 | 4
5 | 5
5 | 6
For an explanation, an element in column ID2 will be mapped to the highest value in the column ID that is less than or equal to the said element in ID2.
For example, 4 in column ID2 is mapped to 3 from column ID, because 3 is the largest value in column ID which is less than or equal to 4.
Is it possible at all to do this in sql?
What I would do is start by joining the two tables on the condition that the id in the first table is less than or equal to that in the second table, like this:
SELECT t1.id, t2.id AS id2
FROM t1
JOIN t2 ON t2.id >= t1.id;
Once you have that, you can select the maximum id from the first table, and group by the id from the second table to get the largest pairs:
SELECT MAX(t1.id) AS id, t2.id AS id2
FROM t1
JOIN t2 ON t2.id >= t1.id
GROUP BY t2.id;
SQL Fiddle seems to be down but I will update with a link as soon as I can.
SELECT MAX(A.ID) ID, B.ID2
FROM A
INNER JOIN B ON B.ID2 >= A.ID
GROUP BY B.ID2
If you only need the matching ID column:
select b.*,
(select max(ID) from a where a.ID <= b.ID2) as a_Id
from b
If you need more columns:
select *
from a
join
(
select b.*,
(select max(ID) from a where a.ID <= b.ID2) as a_Id
from b
) as b
on a.Id = b.a_Id

Selecting record from two tables with INNER join limit 3

I have records in two tables as shown below
Table1
userid email
1 123#qwe.com
4 qwe#sdf.cok
5 sad#fgdf.sdf
7 dsvh#dsf.we
9 fdsdf#fgh.hh
.
Table 2
userid values
1 15
1 45
1 76
1 15345
4 4545
4 76788
4 15879
5 4534
5 76345
5 15678
5 4567
5 7667789
7 15
7 456786
7 76678
7 15678
9 45789
9 76789
9 15789
9 4557
9 7667
9 1556
9 4556
9 764
Now I want the first 3 records from table1 with userid in descending order along with the table2 records related to them
SELECT *
FROM (SELECT TOP 3 * FROM Table1) AS Table1 INNER JOIN
Table2 ON Table1.userid = Table2.userid
ORDER BY Table1.userid
SELECT a.userid, a.email, b.values
FROM table1 a
INNER JOIN table2 b ON a.userid = b.userid
ORDER BY a.userid asc
LIMIT 0,3
SELECT * FROM (
SELECT t1.userid, t1.email, group_concat(t2.`values`) as t2values
FROM table1 t1
INNER JOIN table2 t2 ON (t1.userid = t2.userid)
GROUP BY t1.userid
ORDER BY t1.userid ASC
LIMIT 3 ) subselect
ORDER BY subselect.userid DESC
Explanation:
Group_concat is an aggregate function that will make a list of comma separated values.
Because you are using an aggregate function you need to group on userid.
The limit 3 selects the first 3 userid's (because you've ordered ASC)
Then the outer select picks up all 3 rows and reverses the order to DESC.
Links
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat