I have two tables (Manager and Worker). I want to do a left join from Manager to Worker. The name of workers under a manager should come below manager name. The expected output is presented below:-
Manager Table:
ManagerCode Name Age
1 Chris 52
2 Rick 55
3 David 50
Worker Table
ManagerCode WName Age
1 Harry 33
1 Phil 40
2 Johnny 28
2 Jeff 47
Expected table:
ManagerCode Name Age
1 Chris
1 Harry
1 Phil
2 Rick
2 Johnny
2 Jeff
3 David
Left join results both manager and worker names side by side. But I want them in one column.
You meed UNION ALL and not a join:
select t.managercode, t.name, t.age
from (
select managercode, name, age, 1 as ismanager from Manager
union all
select managercode, wname, age, 0 from Worker
) t
order by t.managercode, t.ismanager desc
The column ismanager is used to sort the manager before all the workers under them.
See the demo.
Results:
| managercode | name | age |
| ----------- | ------ | --- |
| 1 | Chris | 52 |
| 1 | Phil | 40 |
| 1 | Harry | 33 |
| 2 | Rick | 55 |
| 2 | Johnny | 28 |
| 2 | Jeff | 47 |
Related
I have a total number of 10 employees and I have to display the next 3 employees details on my page and I tried the below code.
SELECT *
FROM employee
where empid > 1
and is_active = 1
order
by empid ASC
limit 0 , 3
and I am getting the output 2,3,4 which is correct.
Now my issue is, If the empid is 10 then I am getting the empty row. Is it possible to display the 1,2,3 when the empid is 10 or if the empid is 8 the display 9,10,1
I mean I want it to wrap around from 10 to 1.
empid | Name | Position | Office | Age
1 | Airi | Accountant | Tokyo | 33
2 | Angelica | Officer | London | 47
3 | Ashton | Technical | Francisco| 66
4 | Bradley | Software | London | 41
5 | Brenden | Engineer | Francisco| 28
6 | qodjf | Accountant | Tokyo | 33
7 | Angelica | Officer | New York | 50
8 | Ashton | Technical | Francisco| 30
9 | zxysz | Software | London | 50
10 | Brenden | Engineer | Francisco| 28
I tried to create a table in db-fiddle but I am getting the error. I am using PHPMyAdmin. So i added table here.
Move the condition empid > ? DESC in the ORDER BY clause:
SELECT *
FROM employee
WHERE is_active = 1
ORDER BY empid > ? DESC, empid LIMIT 3
See the demo.
I have database that contains some distinct values like below
id firstName lastName someOption
1 mark tom 28
2 jack bob 75
3 mark tom 48
4 mark tom 87
5 sara tim 64
6 jack bob 23
7 katy jimmy 65
I want to select all the table but filter out distinct records instead of the last one like this
id firstName lastName someOption
4 mark tom 87
5 sara tim 64
6 jack bob 23
7 katy jimmy 65
How to achieve this with sql?
One method is a correlated subquery:
select t.*
from t
where t.id = (select max(t2.id)
from t t2
where t2.firstname = t.firstname and t2.lastname = t.lastname
);
With an index on (lastname, firstname, id), this is probably the fastest method on larger amounts of data.
With NOT EXISTS:
select t.* from tablename t
where not exists (
select 1 from tablename
where (firstName, lastName) = (t.firstName, t.lastName) and id > t.id
)
See the demo.
Results:
| id | firstName | lastName | someOption |
| --- | --------- | -------- | ---------- |
| 4 | mark | tom | 87 |
| 5 | sara | tim | 64 |
| 6 | jack | bob | 23 |
| 7 | katy | jimmy | 65 |
I want a count of the number of rows for each user in the table. For example, suppose I have a table like this:
user_id | order_id | name
--------+----------+-----
6 | a1 | Joe
6 | b4 | Joe
6 | c2 | Joe
6 | d5 | Joe
6 | a6 | Joe
8 | b9 | Mary
8 | c7 | Mary
8 | a2 | Mary
3 | ba | Jack
3 | c3 | Jack
3 | a9 | Jack
3 | b6 | Jack
5 | c9 | Jill
5 | d2 | Jill
I want a result that tells me that Joe appears 5 times, Mary 3 times, Jack 4 times, and Jill twice. So I want a sql statement (preferably mySql, but any language will do), that gives results like this:
name | user_id | count
-----+---------+------
Joe | 6 | 5
Mary | 8 | 3
Jack | 3 | 4
Jill | 5 | 2
Is there a way to do this in sql? I can't find one, but there's a lot I don't know about sql.
Select
Count(userid) as count
, userid
, name
From table
Group by userid, name
Please try:
SELECT COUNT(order_id) AS count, name FROM table_name GROUP BY name
I have three tables like this:
Student
stuNum | stuName
------------------
2012 | jack
2013 | tom
Quiz
quizNum | quizName
------------------
1 | chapter 1
2 | chapter 2
3 | chapter 3
studentassessment
stuNum | quizNum | assessmentMark
-----------------------------------
2012 | 1 | 10
2012 | 2 | 8
2012 | 3 | 10
2013 | 1 | 5
I want to get result something like this
stuNum | stuName | Quiz Num | assessmentMark
--------------------------------------------
2012 | jack | 1 | 10
2012 | jack | 2 | 8
2012 | jack | 3 | 10
description : all three table are connected.. i want to get the stuNum=2012, quizNum that all stuNum have done.
I tried several combination to fetch result but can't work.
this is example combination that i tried :
$sql = "select a.stuNum,a.quizNum,a.assessmentMark from studentassessment a inner join student b on a.stuNum=b.stuNum inner join quiz c on a.quizNum=c.quizNum where b.stuNum='2012'"
Query
SELECT a.stuNum,
b.stuName,
a.quizNum,
c.quizName,
a.assessmentMark
FROM studentAssessment a
JOIN student b
ON a.stuNum=b.stuNum
JOIN quiz c
ON a.quizNum=c.quizNum
WHERE a.stuNum=2012;
Output
+--------+---------+---------+-----------+----------------+
| STUNUM | STUNAME | QUIZNUM | QUIZNAME | ASSESSMENTMARK |
+--------+---------+---------+-----------+----------------+
| 2012 | Jack | 1 | chapter 1 | 10 |
| 2012 | Jack | 2 | chapter 2 | 8 |
| 2012 | Jack | 3 | chapter 3 | 10 |
+--------+---------+---------+-----------+----------------+
Fiddle demo
Here .. try this out
SELECT a.stuNum,b.stuName, a.quizNum, a.assessmentMark
FROM studentassessment a
INNER JOIN student b ON a.stuNum = b.stuNum
WHERE b.stuNum='2012'
you do not need to put an inner join on quiz table since neither you are retrieving any value from it nor you are using it to deduce some relation in your output
I have three Tables
Countries
id | name
1 | USA
2 | UAE
3 | UK
4 | INDIA
Users
id | name | countryid
1 | Philip| 1
2 | Aby | 3
3 | Sam | 3
Happiness
id | userid | mark
1 | 1 | 10
1 | 2 | 50
1 | 3 | 70
I need to get a result of happiness country ranking as
Rank | Country | Total
1 | UK | 120
2 | UAE | 10
3 | USA | 0
4 | INDIA | 0
I need a mysql query for this solution..
Maybe something like this:
SET #rank=0;
SELECT
#rank:=#rank+1 AS rank,
tbl.*
FROM
(
SELECT
Countries.name,
COALESCE(SUM(Happiness.mark),0) AS Mark
FROM
Countries
LEFT JOIN Users
on Countries.id=Users.countryid
LEFT JOIN Happiness
ON Happiness.userid=Users.id
GROUP BY
Countries.Name
) AS tbl
ORDER BY tbl.Mark DESC
References:
MySql - Get row number on select
How do I get SUM function in MySQL to return '0' if no values are found?
SQL fiddle here