I am performing union on two select statements but the result I am getting contains the random rows like some from table one and some from table two.But I want to get first all the rows from table one and then from table two.I am using MySql database.
Table 1
==========================
s_no Name Marks
1 nikhil 25
Table 2
====================
s_no Name Marks
1 Akhil 10
2 Mark 20
1 Kim 40
Here is the query that I am using:
select * from (
select t2.s_no,t2.name,t2.marks from table1 t2
union all
select t1.roll_no,t1.name,t1.marks from table1 t1
) a order by s_no desc
Here are the results:
Actual result
=========================
s_no Name Marks
1 Akhil 10
1 nikhil 25 <<<
1 kim 40
2 mark 20
required result
====================
s_no Name marks
1 Akhil 10
1 Kim 40
1 nikhil 25 <<<
2 mark 20
Try this. You need to add order by s_no,name in the last select query
(select * from table1)
union
(select * from table2) order by s_no,name
Related
For example, I have the following table :
id user_id name age address
1 12 John 21 earth
2 13 Daniel 19 planet
3 12 Paul 25 here
4 11 Joana 23 mars
5 11 Paul 18 earth
The results that I want :
id user_id name age address
1 12 John 21 earth
3 12 Paul 25 here
4 11 Joana 23 mars
5 11 Paul 18 earth
So basically, I want to show all rows from duplicated values in the user_id column. I am new to SQL and hopefully, you guys can help me. Thanks in advance.
You can do something like below.
select * from your_table where user_id in (
select user_id from your_table
group by user_id having count(*) > 1
)
I would recommend exists for this purpose:
select t.*
from t
where exists (select 1 from t t2 where t2.user_id = t.user_id and t2.id <> t.id)
order by user_id, id;
In general, it is best to avoid aggregation functions in subqueries if you can -- for performance reasons.
Here is the table data
Name Stage ID Event ID Attempt No Score
Ramesh 1 1 1 10
Ramesh 1 1 2 20
Ramesh 2 1 1 30
Suresh 1 1 1 05
Suresh 2 1 1 15
Suresh 2 2 1 25
Suresh 2 2 2 35
Suresh 2 2 3 30
We have a Table with Name, Stage ID, Event ID, Attempt No, Score.
We want to group the data for Name, Stage ID, Event ID, Attempt No, Max(Score)
There can be records for every attempt under stage and Event and name and we need to take the max score and the same needs to be displayed.
The output should be :
Name Stage ID Event ID Attempt No Score
Ramesh 1 1 2 20
Ramesh 2 1 1 30
Suresh 1 1 1 05
Suresh 2 1 1 15
Suresh 2 2 2 35
This would have one record for the combination of name + stage + Event
You can try below using correlated subquery
select * from tablename t1
where score in
(select max(score) from tablename t2 where t1.name=t2.name and t1.stage=t2.stage
and t1.eventid=t2.eventid)
You could use a inner join on the subquery for the max value you need
select yt.*
from your_table yt
INNER JOIN (
select Name, Stage_ID, Event_ID, Attempt_No, max(score) max_score
from your_table
group by Name, Stage_ID, Event_ID, Attempt_No
) t on yt.name = t.name
and yt.Stage_ID = t.Stage_ID
and yt.Event_ID = t.Event_ID
and yt.Attempt_No = t.Attempt_No
and yt.score = t.max_score join
I am currently in the need to find entrys matching the same pattern in a connection table.
The Table looks like
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
3 1 1 5
4 1 2 4
5 5 1 3
6 5 2 7
so my basic information is the data of job_id 15
id job_id data1 ext_id
-- ------ ----- -----
1 15 1 3
2 15 2 7
I want to find job_id 5 because the data in ext_id and data1 is the same as in job 15. the data of job_id 1 differs, so I don't want to find that.
Any idea on how to do it?
I believe you want this:
select *
from your_table
group by data1,
ext_id
having count(*) > 1
This post explains it:
How to find duplicates in 2 columns not 1
EDIT
I believe this should return all rows that have mathcing data1 and ext_id values
select * from table t1
INNER JOIN table t2 ON t1.data1=t2.data1 and t1.ext_id=t2.ext_id
I have the table like this
id rate_user_id content_id points category_id
1 100 1 5 1
2 101 1 3 1
3 100 2 8 1
4 103 2 11 1
So I want to looking highest point of content in this category 1
Content_id 2 = 19 points .
U can do this by
select content_id,sum(points) from user group by content_id order by sum(points) desc limit 1;
Thanks
have a look at the command MAX.
http://technet.microsoft.com/en-us/library/ms187751.aspx
Something like
SELECT MAX(Pts) FROM
(SELECT SUM(points) Pts GROUP BY content_id) A
I have a table with the following fields:
id
source_id
title
date
I want to select the 25 most recent items, so SELECT * FROM table ORDER BY date DESC LIMIT 50
The extra requirement is to select only the 3 most recent from every source_id.
So if the records look something like that,
id | source_id | title | date
----+-----------+-------+---------
1 2 aaa 2012-1-1
2 2 aaa 2012-1-2
3 2 aaa 2012-1-3
4 2 aaa 2012-1-4
5 3 aaa 2012-1-5
6 4 aaa 2012-1-6
I want my query to return items 4,3,2,5,6
So just the 3 most recent of every source with an over all limit of 25.
I'm not sure it's clear enough so please ask if you need more details.
Here you go:
SELECT *
FROM your_table t1
WHERE
(
SELECT COUNT(*)
FROM your_table t2
WHERE
t1.source_id = t2.source_id
AND t1.date < t2.date
) < 3
ORDER BY source_id, date DESC
Result:
4 2 aaa 2012-01-04
3 2 aaa 2012-01-03
2 2 aaa 2012-01-02
5 3 aaa 2012-01-05
6 4 aaa 2012-01-06
In plain English: take only rows that have less than 3 newer rows with the same source_id.
NOTE: This could select more than 3 rows per source_id if the third newest date (for the same source_id) happens to be shared by more than one row. Let me know what "3 newest" means in this context if that's a problem...
select * from table where source_id in
(select distinct source_id from table order by date limit 3)
LIMIT 25