Mysql - Join query - mysql

I have the following 2 queries:-
Query 1:-
mysql> select mccid_c, id_c from contacts_cstm where accountnumber_c = '1601480000552527';
250762 | 475093000013882513
Query 2:-
mysql> select first_name, last_name from contacts where id = '475093000013882513';
John | Doe
id in contacts = id_c in contacts_cstm
I required a join query to get mccid_c, first_name, last_name in one query
Thanks!

This is a classic usecase of the join syntax:
SELECT mccid_c, first_name, last_name
FROM contacts_cstm cc
JOIN contacts c ON c.id = cc.id_c
WHERE c.id = '475093000013882513'

You can use multiple tables in your single SQL query. The act of joining in MySQL refers to smashing two or more tables into a single table.
You can use JOINS in SELECT, UPDATE and DELETE statements to join MySQL tables.But firstly you need a common column (attribut),suppose it is 'tutorial_author' between the two tables that you want to join it.
SELECT a.mccid_c, b.first_name , b.last_name
FROM contacts_cstm a, contacts b
WHERE a.tutorial_author = b.tutorial_author and a.accountnumber_c = '1601480000552527' and b.id = '475093000013882513';
I hope it will help you, best regards

Try this.
Select a.mccid_c, b.first_name, b.last_name from contacts_cstm a inner join contacts b where a.id_c=b.id;

Related

MYSQL Joining two tables with one identical column

I have two tables: table 1 = university and table 2 = school
I added university_id into the table 2 and I need to connect the two tables.
If university_name from table 1 and name from table 2 are identical, get the id from table 1 and replace it onto table 2 university_id
I am very new to sql so if you could explain that would be great. I have also tried the following with no avail!
select a.id,b.name from university as a
inner join school as b on a.university_name = b.name
UPDATE `school` SET `university_id` = a.id WHERE a.university_name = b.name
Something like
UPDATE school a
JOIN university b ON a.university_name = b.name
SET a.university_id = b.id
should work
I cannot run a test right now... Maybe it does give you a hint.
UPDATE `school` s SET `university_id` = (SELECT u.id FROM `university` u WHERE u.name=s.university_name)
You might need to JOIN the school-table within the SELECT statement.

SQL Statement to retrieve information from multiple tables

I need some help constructing a SQL Statement.
I just have basic knowledge concerning SQL (insert, delete, alter, where, select) but never really had to deal with aggregate functions or joins.
So here is the setup:
TABLE A
cust_id
prod_id
statusCode
...
TABLE B
cust_id
land_id
...
TABLE C
country_id
...
TABLE D
country_id
country_code
TABLE E
product_id
country_code1
What the SQL Statement should output is: All rows where the statusCode from Table A is 1, or 2, or 3 and where the country_code == country_code1.
Where country_code can obtained via Table B,C,D and country_code1 via Table E.
Please do not post answers concerning the database structure itself since i have no rights to change them.
My approach was this but it is clear that it is horribly wrong since I am a SQL beginner:
SELECT * FROM TableA
INNER JOIN TableB ON TableA.cust_id = TableB.cust_id
INNER JOIN TableC ON TableB.landId = TableC.country_id
INNER JOIN TableE ON TableA.prod_id = TableE.product_id
INNER JOIN TableD ON TableE.country_code1 = TableD.country_code
WHERE statusCode IN (1,2,3)
Off the top of my head.
Join the two groups of tables with FK
Join those groups,
Restrict that super set
more to come
SELECT *
FROM (tableA A INNER JOIN tableB B ON A.cust_id=B.cust_id)
INNER JOIN tableE E ON E.product_id=A.prod_id
INNER JOIN (tableC C INNER JOIN tabldeD D ON D.country_id)
ON D.country_code = E.country_code1
WHERE A.statusCode IN(1,2,3)
We don't have to worry about the country code bit because it is in the 'join'.
Off the top of my head (untested, and I'm not 100% sure that this is what your requirement is):
select a.cust_id, a.prod_id, a.statusCode,
b.land_id,
c.country_id,
d.country_code,
e.product_id
from TableA a, TableB b, TableC c, TableD d, TableE e
where a.cust_id = b.cust_id -- basic joins on common fields
and b.land_id = c.country_id
and b.land_id = d.country_id
and d.country_code = e.country_code1
and a.statusCode in (1, 2, 3) -- finally the statuscode selection

SQL SELECT name by id

I need help with a sql query.
I have these 2 tables:
player_locations:
ID | playerid | location <- unqiue key
---|-----------------------
1 | 1 | DOWNTOWN
and users:
ID | playername | [..]
----|--------------------
1 | example1 | ...
I need a select to get the users.playername from the player_locations.playerid. I have the unique location to get the player_locations.playerid.
Pseudo query:
SELECT playername
FROM users
WHERE id = player_locations.playerid
AND player_locations.location = "DOWNTOWN";
The output should be example1.
This is just a simple INNER JOIN. The general syntax for a JOIN is:
SELECT stuff
FROM table1
JOIN table2 ON table1.relatedColumn = table2.relatedColumn
In your case, you can relate the two tables using the id column from users and playerid column from player_locations. You can also include your 'DOWNTOWN' requirement in the JOIN statement. Try this:
SELECT u.playername
FROM users u
JOIN player_locations pl ON pl.playerid = u.id AND pl.location = 'DOWNTOWN';
EDIT
While I personally prefer the above syntax, I would like you to be aware of another way to write this which is similar to what you have now.
You can also select from multiple tables by using a comma in your FROM clause to separate them. Then, in your WHERE clause you can insert your conditions:
SELECT u.playername
FROM users u, player_locations pl
WHERE u.id = pl.playerid AND pl.location = 'DOWNTOWN';
Here is the solution.
SELECT
playername
FROM users
WHERE id = (SELECT id FROM player_locations WHERE location='DOWNTOWN')
I have a idea, try this:
SELECT playername
FROM users
WHERE id IN (SELECT DISTINCT playerid FROM player_location WHERE location LIKE "DOWNTOWN");

Add columns when using IN in mysql for the result

Following is my table for artists -
id name sex
1 harsh male
2 geet female
Following is my table for events -
id artist_id created_by
2 2 16
2 2 17
Following is my query -
SELECT * FROM `events` WHERE artist_id IN (SELECT id FROM `artists` WHERE name LIKE '%$search_term%')
But apart from events data I need to get artist name in the result as well, please let me know what I need to change in my query as I tried *, artists.name it wont worked.
Try this query
SELECT e.*
FROM `artists` AS a
JOIN `events` AS e ON e.artist_id = a.id
WHERE a.name LIKE '%$search_term%';
You need to select from two tables simultaneously. Use the join for that
SELECT artists.name, events.*
FROM artists
INNER JOIN events
ON artist.id = artist_id
WHERE
name LIKE '%search_term%'
Use a join instead of an IN
SELECT
e.*,
artists.name
FROM
`events` e
inner join `artists` a on e.artist_id = a.id
WHERE
name LIKE '%$search_term%'

Joining Tables in MySQL

I have two tables namely Students & Hobbies with following structure and records:
ID,Name
-----------
9,Peter
10,Steve
ID,Hobby
-----------------
9,dancing
9,singing
10,learning
I want to JOIN these tables and get a unique record from table Students.
I am doing this right now which evolves duplicate records:
SELECT a.Name
FROM Students a
LEFT JOIN Hobbies h ON a.ID =h.ID
This gives :
Name
----------
Peter
Peter
Steve
I got the reason, this is because, the table Hobbies has two records of ID=9 , that is why duplicate records are evolved, but how to retrieve a single record? Please help.
I want this:
Name
-----------
Peter
Steve
SELECT a.ID, a.Name
FROM Students a
LEFT JOIN Hobbies h
ON a.ID = h.ID
GROUP BY a.ID, a.Name
With the GROUP BY, you could then answer questions like "How many hobbies does each student have?"
SELECT a.ID, a.Name, COUNT(*) AS Number_of_Hobbies
FROM Students a
LEFT JOIN Hobbies h
ON a.ID = h.ID
GROUP BY a.ID, a.Name
This is really not the way a JOIN should be used.
left joining an unreferenced table on a constant gets you nothing.
the answer to the question you asked is "use a DISTINCT clause":
SELECT DISTINCT a.Name FROM Students a LEFT JOIN Hobbies h ON a.ID = 9
...but really, I'm pretty sure this is not what you want to do.
based off the comments, I believe what the OP's intent was something like this:
SELECT
a.Name
FROM
Students a
WHERE
a.ID = <Student ID>;
... AND , for the hobbies reference:
SELECT DISTINCT
a.ID,
a.Name
FROM
Students a
INNER JOIN
Hobbies h ON h.ID = a.ID
WHERE
h.Hobby = <Hobby Name>;
Use the distinct keyword:
SELECT distinct a.Name
FROM Students
etc....
It eliminates duplicate rows, so you'll only get one of each name.
You are getting duplicates because Peter has 2 hobbies. If you want a single record per User do this:
select * from Student where Id in (
Select DISTINCT Id from HobbY)