listing corresponding data of two tables - mysql

i'm pretty new to sql and i have a problem that i coundn't really describe to google
SELECT name AS state
FROM state
WHERE id IN
(
SELECT state_id
FROM city
WHERE id=(SELECT city_id FROM zipcode)
)
;
This lists the states of all zipcodes I have in my database, I also want to list the zipcodes (zipcode.zipcode) in a additional column corresponding with the state
Thank you very much in advance for your help
my tables look like this:
city
+----+----------+-----------+---------------------+---------+---------+
| id | state_id | county_id | name | lat | lng |
+----+----------+-----------+---------------------+---------+---------+
| 1 | 1 | 1 | Prem, Oberbayern | 47.6833 | 10.8 |
| 2 | 2 | 2 | Pfullendorf (Baden) | 47.9249 | 9.25718 |
| 3 | 3 | 3 | Wissen, Sieg | 50.7833 | 7.75 |
| 4 | 1 | 4 | Miltenberg | 49.7039 | 9.26444 |
| 5 | 1 | 5 | Moosthenning | 48.6833 | 12.5 |
| 6 | 1 | 1 | Bernbeuren | 47.7333 | 10.7833 |
| 7 | 4 | 6 | Demmin, Hansestadt | 53.9 | 13.0333 |
| 8 | 2 | 7 | Konstanz, Universit | 47.6667 | 9.18333 |
| 9 | 5 | 8 | Teutschenthal | 51.45 | 11.8 |
| 10 | 6 | 9 | Vierlinden | 52.515 | 14.3141 |
+----+----------+-----------+---------------------+---------+---------+
zipcode
+----+---------+-------------+---------+
| id | city_id | district_id | zipcode |
+----+---------+-------------+---------+
| 1 | 1 | NULL | 86984 |
| 2 | 2 | NULL | 88630 |
| 3 | 3 | NULL | 57537 |
| 4 | 4 | NULL | 63897 |
| 5 | 4312 | 502 | 84164 |
| 6 | 6 | NULL | 86975 |
| 7 | 7 | 778 | 17109 |
| 8 | 8 | NULL | 78462 |
| 9 | 8 | NULL | 78464 |
| 10 | 8 | NULL | 78465 |
+----+---------+-------------+---------+
state
+----+------------------------+
| id | name |
+----+------------------------+
| 1 | Bayern |
| 2 | Baden-W?rttemberg |
| 3 | Rheinland-Pfalz |
| 4 | Mecklenburg-Vorpommern |
| 5 | Sachsen-Anhalt |
| 6 | Brandenburg |
| 7 | Niedersachsen |
| 8 | Schleswig-Holstein |
| 9 | Nordrhein-Westfalen |
| 10 | Th?ringen |
+----+------------------------+

Use explicit JOINs:
SELECT z.*, s.name AS state
FROM zipcode z JOIN
city c
ON z.city_id = c.id JOIN
state s
ON c.state_id = s.id;
If you are learning SQL, focus on learning JOIN rather than IN.

Related

how to sort sql data for given table?

+---------+----------------+--------+
| aid | fn | col_no |
+---------+----------------+--------+
| 2011768 | ABDUL | 5 |
| 2011499 | ABDULLA | 4 |
| 2011198 | ADNAN | 3 |
| 2011590 | AKSHAYA PRAISY | 2 |
| 2011749 | AMIR | 1 |
| 2011213 | AMOGHA | 5 |
| 2011027 | ANU | 4 |
| 2011046 | ANUDEV D | 3 |
| 2011435 | B S SAHANA | 2 |
| 2011112 | BENAKA | 1 |
+---------+----------------+--------+
How to sort the number like col_no as 1 2 3 4 5 and again repeat as 1 2 3 4 5?
i need output like this
+---------+----------------+--------+
| aid | fn | col_no |
+---------+----------------+--------+
| 2011749 | AMIR | 1 |
| 2011590 | AKSHAYA PRAISY | 2 |
| 2011198 | ADNAN | 3 |
| 2011499 | ABDULLA | 4 |
| 2011768 | ABDUL | 5 |
| 2011112 | BENAKA | 1 |
| 2011435 | B S SAHANA | 2 |
| 2011046 | ANUDEV D | 3 |
| 2011027 | ANU | 4 |
| 2011213 | AMOGHA | 5 |
+---------+----------------+--------+
You can use row_number() partition by col_no:
select t.*
from t
order by row_number() over (partition by col_no order by fn),
col_no;
Here is a db<>fiddle.

Display same column multiple times on one row

i'm trying to display the same column three times with different values, but can't seem to get the code. right.
I have three tables
Projectdetails (projectID), projectroles (projectroleID), projectstaff (projectstaffID)
Projectdetails
+-----------+-------------+----------------+---------------+----------------+
| ProjectID | ProjectCode | ProjectName | ProjectActive | ProjectStageID |
+-----------+-------------+----------------+---------------+----------------+
| 1 | K6674 | Ironworks | TRUE | 5 |
| 2 | K6694 | Overstone Leys | TRUE | 5 |
| 3 | K6702 | Salt | TRUE | 5 |
| 9 | K6678 | Dyers | TRUE | 5 |
| 10 | K6397 | Chatham Road | TRUE | 6 |
| 11 | K6691 | Kennel Grounds | TRUE | 1 |
| 12 | K6693 | Church Farm | TRUE | 4 |
+-----------+-------------+----------------+---------------+----------------+
ProjectStaff
+----------------+-----------+------------+---------------+
| ProjectStaffID | ProjectID | EmployeeID | ProjectRoleID |
+----------------+-----------+------------+---------------+
| 1 | 1 | 31 | 6 |
| 2 | 2 | 15 | 6 |
| 3 | 3 | 61 | 6 |
| 4 | 9 | 61 | 6 |
| 5 | 10 | 72 | 6 |
| 6 | 10 | 95 | 13 |
| 7 | 10 | 70 | 10 |
| 8 | 10 | 1 | 10 |
+----------------+-----------+------------+---------------+
ProjectRoles
+---------------+---------------------------+
| ProjectRoleID | ProjectRole |
+---------------+---------------------------+
| 1 | Archaeologist |
| 2 | Assistant Supervisor |
| 3 | CAD Manager |
| 4 | CAD Technician |
| 5 | Photographer |
| 6 | Project Manager |
| 7 | Project Officer |
| 8 | Senior Project Officer |
| 9 | Specialist |
| 10 | Supervisor |
| 11 | Surveyor |
| 12 | Trainee |
| 13 | Post-Excavation Manager |
+---------------+---------------------------+
SELECT
projectdetails.ProjectID,
projectdetails.ProjectCode,
projectdetails.SiteCode,
(SELECT
employees.EmpABB
WHERE
projectstaff.ProjectRoleID = 6) AS ProjectManager,
(SELECT
employees.EmpABB
WHERE
projectstaff.ProjectRoleID = 13) AS PostExManager,
(SELECT
employees.EmpABB
WHERE
projectstaff.ProjectRoleID = 10) AS Supervisor,
projectdetails.ProjectName,
projectdetails.ProjectActive,
projectdetails.ProjectStageID,
projectdetails.AuthorityID
FROM
projectdetails
LEFT JOIN projectstaff ON projectstaff.ProjectID = projectdetails.ProjectID
INNER JOIN projectroles ON projectstaff.ProjectRoleID = projectroles.ProjectRoleID
INNER JOIN employees ON employees.ProjectRoleID = projectroles.ProjectRoleID AND projectstaff.EmployeeID =
employees.EmployeeID
GROUP By
projectdetails.projectID
This produces the following table. It's displaying each project once but not displaying data for postex manager or Supervisor.
Example below:
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| ProjectID | ProjectCode | SiteCode | ProjectManager | PostExManager | Supervisor | ProjectName | ProjectActive | ProjectStageID |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 1 | K6674 | RHN20 | AG | | | Ironworks | TRUE | 5 |
| 2 | K6694 | | PC | | | Overstone Leys | TRUE | 5 |
| 3 | K6702 | | PCM | | | Salt | TRUE | 5 |
| 9 | K6678 | | PCM | | | Dyers | TRUE | 5 |
| 10 | K6397 | KCWC19 | ZP | | | Chatham Road | TRUE | 6 |
| 11 | K6691 | KAKW20 | JW | | | Kennel Grounds | TRUE | 1 |
| 12 | K6693 | | PM | | | Church Farm | TRUE | 4 |
| | | | | | | | | |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
But if I remove GROUP BY, it does display the other project Roles but separates out the data by adding additional rows.
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| ProjectID | ProjectCode | SiteCode | ProjectManager | PostExManager | Supervisor | ProjectName | ProjectActive | ProjectStageID |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 1 | K6674 | RHN20 | AG | | | Ironworks | TRUE | 5 |
| 2 | K6694 | | PC | | | Overstone Leys | TRUE | 5 |
| 3 | K6702 | | PCM | | | Salt | TRUE | 5 |
| 9 | K6678 | | PCM | | | Dyers | TRUE | 5 |
| 10 | K6397 | KCWC19 | ZP | | | Chatham Road | TRUE | 6 |
| 10 | K6397 | KCWC19 | | JB | | Kennel Grounds | TRUE | 6 |
| 10 | K6397 | KCWC19 | | | W Pool | Church Farm | TRUE | 6 |
| 10 | K6397 | KCWC19 | | | R Books | March House | TRUE | 6 |
+-----------+-------------+----------+----------------+---------------+----------
Any ideas?
I'm going to answer my own question. Usual thing, you ask the question, relax, then the answer pops in your head. I'm not entirely sure why it works as I got the answer from another query in my list of queries which have worked before.
You use CASE
SELECT
projectdetails.ProjectID,
projectdetails.ProjectCode,
projectdetails.SiteCode,
max(case when projectstaff.ProjectRoleID = 6 then employees.EmpABB else null end) as ProjectManager,
max(case when projectstaff.ProjectRoleID = 13 then employees.EmpABB else null end) as PostExManager,
max(case when projectstaff.ProjectRoleID = 10 then employees.EmpABB else null end) as Supervisor,
projectdetails.ProjectName,
projectdetails.ProjectActive,
projectdetails.ProjectStageID,
projectdetails.AuthorityID
FROM
projectdetails
LEFT JOIN projectstaff ON projectstaff.ProjectID = projectdetails.ProjectID
INNER JOIN projectroles ON projectstaff.ProjectRoleID = projectroles.ProjectRoleID
INNER JOIN employees ON employees.ProjectRoleID = projectroles.ProjectRoleID AND projectstaff.EmployeeID =
employees.EmployeeID
GROUP BY
projectdetails.ProjectID
| ProjectID | ProjectCode | SiteCode | ProjectManager | PostExManager | Supervisor | ProjectName | ProjectActive | ProjectStageID |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 1 | K6674 | RHN20 | AG | | | Ironworks | TRUE | 5 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 2 | K6694 | | PC | | | Overstone Leys | TRUE | 5 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 3 | K6702 | | PCM | | | Salt | TRUE | 5 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 9 | K6678 | | PCM | | | Dyers | TRUE | 5 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 10 | K6397 | KCWC19 | ZP | JB | W Pool | Chatham Road | TRUE | 6 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 11 | K6691 | KAKW20 | JW | | | Kennel Grounds | TRUE | 1 |
+-----------+-------------+----------+----------------+---------------+------------+----------------+---------------+----------------+
| 12 | K6693 | | PM | | | Church Farm | TRUE | 4 |
+-----------+-------------+----------+----------------+---------------+------------+-----

Join two tables on nearest matching string

I have a query_table Table and wants to join with match_table Table with nearest matching string. If it was vice-versa then 'like' would have worked but have no idea how to do this.
query_table
+----+------------------+
| id | string |
+----+------------------+
| 1 | fcc9e8796feb |
| 2 | fcdbd7ebcf89 |
| 3 | fccc87896feb |
| 4 | fcc7c7896fef |
| 5 | fcced777aaaf |
+----+------------------+
match_table
+----+-----------+
| id | match_code|
+----+-----------+
| 1 | fcff |
| 2 | fcccc |
| 3 | fccc8 |
| 4 | fccc9 |
| 5 | fccdb |
| 6 | fccdc |
| 7 | fccd8 |
| 8 | fcce |
| 9 | fcced |
| 10 | fccee |
| 11 | fcce6 |
| 12 | fcc7b |
| 13 | fcc7c |
| 14 | fcc8e |
| 15 | fcc87 |
| 16 | fcc88 |
| 17 | fcc9e |
| 18 | fcdbb |
| 19 | fcdbc |
| 20 | fcdbd |
+----+-----------+
I expect
result
+----+------------------+----+----------------+
| id | string | id | match_code |
+----+------------------+----+----------------
| 1 | fcc9e8796feb | 17 | fcc9e |
| 2 | fcdbd7ebcf89 | 20 | fcdbd |
| 3 | fccc87896feb | 3 | fccc8 |
| 4 | fcc7c7896fef | 13 | fcc7c |
| 5 | fcced777aaaf | 9 | fcced |
+----+------------------+----+----------------+

I want to ranking result with city based in mysql

I want to ranking result with city based in mysql
table1:-users
| user_id | marks |
--------------------
| 1 | 10 |
| 5 | 10 |
| 5 | 50 |
| 3 | 15 |
| 4 | 10 |
| 2 | 10 |
| 6 | 10 |
| 6 | 50 |
| 4 | 15 |
| 4 | 10 |
table:-2 users details
| user_id | city |
--------------------
| 1 | newdelhi |
| 2 | kolkata |
| 3 | mumbai |
| 4 | newdelhi |
| 5
| 6 | newdelhi |
I want to result like this:
| user_id | points |
--------------------
| 6 | 60 |
| 4 | 35 |
| 1 | 10 |
Try this :
SELECT
users.user_id
,SUM(users.marks) AS points
FROM
users
INNER JOIN
users_details ON users.user_id = users_details.user_id
WHERE
users_details.city = 'newdelhi'
GROUP BY
user_id

How to delete some specify row from a table

I have a test table
mysql> select * from test1 limit 20;
+------+------+----------------------------------+
| id | num | pass |
+------+------+----------------------------------+
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 3 | c81e728d9d4c2f636f067f89cc14862c |
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 2 | c81e728d9d4c2f636f067f89cc14862c |
| 1 | 1 | c4ca4238a0b923820dcc509a6f75849b |
| 2 | 3 | c81e728d9d4c2f636f067f89cc14862c |
| 3 | 3 | eccbc87e4b5ce2fe28308fd9f2a7baf3 |
| 4 | 6 | a87ff679a2f3e71d9181a67b7542122c |
| 5 | 7 | e4da3b7fbbce2345d7772b0674a318d5 |
| 6 | 6 | 1679091c5a880faf6fb5e6087eb1b2dc |
| 7 | 8 | 8f14e45fceea167a5a36dedd4bea2543 |
| 8 | 11 | c9f0f895fb98ab9159f51fd0297e236d |
| 9 | 10 | 45c48cce2e2d7fbdea1afc51c7c6ad26 |
| 10 | 12 | d3d9446802a44259755d38e6d163e820 |
| 11 | 12 | 6512bd43d9caa6e02c990b0a82652dca |
| 12 | 18 | c20ad4d76fe97759aa27a0c99bff6710 |
...
I want to delete the 1st,3rd and 9th row.So I use such command
delete from test1 limit 1,3,9;
But it doesn't work,is there any workaround can do this?