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;
Related
I have a table which is having some report info. In that table I have userid and areaname .
Now my requirement is to get the count of user's list based on area
For example table will have
userid | areaname
-----------------
1 |area 1
1 |area 1
2 |area2
2 |area 2
2 |area2
3 |area1
3 |area1
4 |area3
5 |area2
---------------
Result must be
area1 2users
area2 2users
area3 1user
what is the mysql query to achieve this?
Use the Aggregate function COUNT() to get the number of users and GROUP BY to get the count based on areaname
Use DISTINCT for unique values
SELECT COUNT(DISTINCT userid),areaname FROM tablename GROUP BY areaname;
SELECT count(userid),area FROM YOURTABLENAME GROUP BY area;
I see, you can have dublicate entries ad well:
So I would make a sub Select
SELECT sum(partly_sum), area
FROM (
SELECT
userid, area , count(*) as partly_sum
FROM
_YOUR_TABLE_NAME_
WHERE 1
GROUP BY area, userid
) as a_bad_sub_query
WHERE 1
GROUP BY area
Regards
You can use distinct to avoid dupe entries
SELECT COUNT(DISTINCT userid),areaname FROM tablename GROUP BY areaname
I have a table that contains information about "teams". Teams can have subteams - which has been implemented by each row having a "parent_id" column that contains the "team_id" of that subteam's parent. Here's an example of the data:
team_id team_name parent_id
1 sales (null)
2 executives (null)
3 emea sales 1
4 apac sales 1
5 uk sales 3
What I'm trying to achieve is simply to select all rows in the table in order of team_id, BUT I want subteams returned right after their parent team, also in team_id order. So, for the above example data, I would want the data returned in this order:
team_id team_name parent_id
1 sales (null)
3 emea sales 1
5 uk sales 3
4 apac sales 1
2 executives (null)
I've spent several hours wondering how to achieve this, and really haven't come up with anything useful! I would appreciate any pointers on how to approach this.
Note, I am working with an existing project and can't really make drastic database/implementation changes. I'm using PHP and Oracle with the use of stored procedures, which are new to me.
You can use a hierarchical query with the ORDER SIBLINGS BY clause, like so:
WITH teams AS (SELECT 1 team_id, 'sales' team_name, NULL parent_id FROM dual UNION ALL
SELECT 2 team_id, 'executives' team_name, NULL parent_id FROM dual UNION ALL
SELECT 3 team_id, 'emea sales' team_name, 1 parent_id FROM dual UNION ALL
SELECT 4 team_id, 'apac sales' team_name, 1 parent_id FROM dual UNION ALL
SELECT 5 team_id, 'uk sales' team_name, 3 parent_id FROM dual)
SELECT team_id,
team_name,
parent_id
FROM teams
CONNECT BY PRIOR team_id = parent_id
START WITH parent_id IS NULL
ORDER SIBLINGS BY team_id;
TEAM_ID TEAM_NAME PARENT_ID
---------- ---------- ----------
1 sales
3 emea sales 1
5 uk sales 3
4 apac sales 1
2 executives
I have three tables where table_2 is the middle(connected) between table_1 and table_3
tables
table_id
...
...
table_rest
rest_id
table_id
...
rest
rest_id
...
...
And the query to select I use
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
WHERE rest_id = '$rest_id'
What I need now is to join in this query another table reserv
id
...
status
To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.
EDIT: Sample scenario
tables
table_id
1
2
3
4
5
rest
rest_id
1
2
table_rest
table_id | rest_id
1 2
2 2
3 2
4 2
5 2
So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1
So now I have another table
reserv
id | status
1 0
2 1
3 2
So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.
Hope is a little bit more clear now.
You have to point from table reserv to which table is beign booked, let's call it reserv.table_id
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
left join reserv
on reserv.table_id = m.id
WHERE rest_id = '$rest_id'
and reserv.status is null (*note)
*note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =
It's better if you provide sample data or sqlfiddle. Based on what I realize: Is this what you want:
select tables.table_id, rest.rest_id
from tables
left join table_rest on table_rest.table_id = tables.table_id
left join rest on rest.rest_id = table_rest.rest_id
where rest.rest_id = '$rest_id'
and tables.table_id not in (select id from reserv)
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
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;