I have two table with an column 'minute'.
These table are joined with a INNER JOIN.
Now I want to print out all the records from both tables sorted on both the columns 'minute'
Example
Table 1: name - minute
John - 1
Marc - 3
Table 2: name - minute
Gareth - 2
Joe - 3
Output:
John, Gareth, Marc, Joe
The two tables have to remain two separate tables.
You should use UNION :-
(SELECT *
FROM table1)
UNION
(SELECT *
FROM table2)
ORDER BY minute ASC
Output:-
john 1
gareth 2
marc 3
joe 3
Try this
SELECT name
FROM
(
SELECT name, [minute],1 AS sort FROM Table1
UNION ALL
SELECT name, [minute],2 AS sort FROM Table2
) T
ORDER BY [minute],sort
output
name minute
John 1
Gareth 2
Marc 3
Joe 3
OR
SELECT name, [minute]
FROM
(
SELECT name, [minute] FROM Table1
UNION ALL
SELECT name, [minute] FROM Table2
) T
ORDER BY [minute]
output
name minute
John 1
Gareth 2
Joe 3
Marc 3
Try this:
Select name
From table1
Union all
Select name
From table2
order by minute
Not sure if this is what you are looking for
SELECT name, minute FROM table1, table2 ORDER BY table1.minute,table2.minute ASC;
Related
I have a table where the data are as follow
ID Classroom Person
1 1 Alfred
2 1 Maria
3 2 Maria
4 2 Zoe
5 2 Alfred
6 3 Nick
7 3 Paul
8 3 Mike
9 3 Alfred
10 4 Zoe
11 4 Maria
I want to select and return only the Classroom that has as Person only 'Alfred' and 'Maria'
Following statement :
Select * from table_name where (Person='maria') and (Person=Alfred')
doesn't seem to work.
You can see a SQL Fiddle here,
You can use group by and having:
select classroom
from table t
group by classroom
having count(*) = 2 and
sum(person in ('maria', 'Alfred')) = 2;
This assumes that one person cannot be in a classroom multiple times.
This checks that there are two names in the classroom and they are for the two names of interest. If you can have duplicates, you would want:
having count(distinct name) = 2 and
count(distinct case when person in ('maria', 'Alfred') then person end) = 2;
Try this. Group by and having with Count should work.
SELECT Classroom
FROM tablename
WHERE Person IN( 'maria', 'Alfred' )
GROUP BY classroom
HAVING Count(Person) = 2
Table "subjects" contains columns:
sub_date, sub_content, student_id
2014-10-03, english, 1
2014-10-09, maths, 2
2014-10-11, biology, 1
and
Table games contains columns:
game_date, game_content, student_id
2014-10-05, Hockey, 1
2014-10-18, Tennis, 1
2014-10-20, Cricket, 2
I want to display all details of student_id 1 order by date (considering both sub_date and game_date
Date, Topic
2014-10-03, english
2014-10-05, Hockey
2014-10-11, biology
2014-10-18, Tennis
Please help
you could try this:
select sub_date,sub_content from subjects where student_id = 1
UNION
select game_date,topic from games where student_id = 1
You can try with this (even though sounds not such as a good idea):
SELECT tmp.sub_date AS Date,tmp.sub_content AS Topic
FROM (
SELECT sub_date,sub_content,student_id FROM subjects
UNION SELECT game_date,game_content,student_id FROM games
) tmp
WHERE tmp.student_id = 1 ORDER BY tmp.sub_date;
In my table 1 I have something like this
name | age
George 42
Bob 30
Ken 23
In my table 2, I have something like this, this is where i store votes for each person.
name | votes |
George 1
Ken 1
George 1
George 1
Ken 1
My goal is to combine the 2 tables, and return all the rows in table 1 even it doesn't exist in table 2.
Desire results:
name | age | total_votes
George 42 3
Bob 30 0
Ken 23 2
But instead I get:
name | age | total_votes
George 42 3
Ken 23 2
I have tried something like this
SELECT `table_1`.*, coalesce(COUNT(`table_2`.votes), 0) AS total_votes
FROM `table_1`
LEFT JOIN `table_2`
ON `table_1`.name = `table_2`.name
You can do one of these:
1) Use Right Join instead of current Left Join.
Or
2) Exchange table1 and table2 places in your join expression, like:
FROM table_2
LEFT JOIN table_1
Try this. This works in MS Access , I think this will work on your's too just convert the query to SQL:
SELECT Table1.name, First(Table1.age) AS age, Count(Table2.Votes) AS totalVotes
FROM Table1 LEFT JOIN Table2 ON Table1.name = Table2.name
GROUP BY Table1.name;
Left Join table1 to table2 so that all entry from table1 , even if its is corresponding data is null, will be included. GROUP BY your query by name so that votes will be counted by name .
I have table A:
id username firstname event_date
1 Ben lori 2014-01-27
2 Ben lori 2014-01-04
3 Mary john 2014-01-28
I have table B:
id username event_date
1 Ben 2014-01-23
2 Nicole 2014-01-26
I want the result to be like this: Is there any solution that allow me to combine and sort it according to event_date like one table,like this:
2 Ben lori 2014-01-04
1 Ben 2014-01-23
2 Nicole 2014-01-26
1 Ben lori 2014-01-27
3 Mary john 2014-01-28
so that I can use something like while loop or mysql_fetch_assoc to display the content
Basically, you need union all:
select id, username, firstname, event_date
from ((select id, username, firstname, event_date
from tablea
) union all
(select id, username, NULL as firstname, event_date
from tablea
)
) t
order by event_date;
Maybe you are looking for something like this:
SELECT *
FROM tableA FULL OUTER JOIN tableB
ORDER BY event_date
I have an existing table with millions of entries (growing) that consists of:
userid|name|etc...
1 frank ...
1 frank ...
2 joe ...
5 sam ...
1 franky ...
What I need to do is return a table of:
place|name|total
1 franky 3
2 sam 1
3 joe 1
Where total is the SUM(userid = the distinct userid).
Currently I'm doing a query to SELECT DISTINCT userid from table and then foreach returned value in php, I'm doing another query to return the name and sum(userid = userid).
As you can assume, this is very taxing and takes a long time now with all of the values. Is there any way to speed this up by doing 1 query?
i think you need
SELECT #a:=#a+1 AS `place`, name, COUNT(userid) AS `total`
FROM `your_table`, (SELECT #a:= 0) AS a
GROUP BY userid
SELECT userid, COUNT(*)
FROM some_table
GROUP BY userid