i have table like this:
table1:
id | item_name | entered_by | modify_by
1 | banana | 2 | 1
2 | apple | 4 | 3
3 | orance | 1 | 1
4 | pineapple | 5 | 3
5 | grape | 6 | 1
table2:
id | username
1 | admin
2 | jack
3 | danny
4 | dummy
5 | john
6 | peter
how do i join these 2 table for table1's entered_by and modify_by is replaced by their username with id correspondingly on table2.
thanks
Try this out:
SELECT t1.id, t1.item_name,
t2enteredBy.username enteredBy,
t2modifyBy.username modifyBy
FROM table1 t1
JOIN table2 t2enteredBy ON t1.entered_by = t2enteredBy.id
JOIN table2 t2modifyBy ON t1.modify_by = t2modifyBy.id
Fiddle here.
In short, you need a join per each of those fields. That's why there is a double join on table2.
SELECT tmp.id, item_name, tmp.username as entered, b.username as modify
FROM (SELECT t.id, item_name, username, modify_by
FROM table1 t
INNER JOIN table2 a
ON t.entered_by=a.id
)tmp
INNER JOIN table2 b ON tmp.modify_by=b.id
Related
I have a mysql table that looks something like this:
id | name
---+-------
1 | cola
2 | pepsi
3 | sprite
and another table:
customer | buy1 | buy2
---------+------+-----
Jhon | 2 | 3
Alice | 1 | 3
Tony | 3 | 2
I want to join the two tables and generate
customer | buy1 | buy2
---------+-------+--------
Jhon | Pepsi | Sprite
Alice | Cola | Sprite
Tony | Sprite| Pepsi
SELECT C.customer, REF.NAME, REF2.NAME
FROM OTHER_TABLE AS C
JOIN TABLE_SOMETHING_LIKE_THIS AS REF ON C.BUY1 = REF.ID
JOIN TABLE_SOMETHING_LIKE_THIS AS REF2 ON C.BUY2 = REF2.ID
You can write subqueries in a select list like this:
select
customer,
(select name from tbl1 where id = buy1) buy1,
(select name from tbl1 where id = buy2) buy2
from
tbl2;
Im trying to combine distinct and max assign_id or last assign_id in table below but doesnt get a right value.
Table_task
ASSIGN_ID | DRV_ID | VEHICLE_ID
--------------------------------------
1 | EFFA | 1000
2 | SAM | 1001
3 | FIZA | 1004
4 | JIJO | 1000
5 | LISA | 1000
How to get value display as below ?
ASSIGN_ID | DRV_ID | VEHICLE_ID
-----------------------------------------
2 | SAM | 1001
3 | FIZA | 1004
5 | LISA | 1000
Assuming assign_id doesn't have a ties, then you can use subquery :
select t.*
from table t
where assign_id = (select max(t1.assign_id)
from table t1
where t1.vehicle_id = t.vehicle_id
);
Have a sub-query that returns each vehicle_id's max assign_id. JOIN with that result:
select t1.*
from task t1
join (select vehicle_id, max(assign_id) assign_id
from task
group by vehicle_id) t2
on t1.vehicle_id= t2.vehicle_id
and t1.assign_id = t2.assign_id
I am creating an application for my school and I am in trouble constructing the right query.
I have 2 tables,table1 and table2.
table1
---------------------------------------------------------
| StudentID | SubjectID | Present | Type |
---------------------------------------------------------
| 2 | 3 | yes | 1 |
| 2 | 2 | yes | 2 |
| 3 | 1 | no | 3 |
---------------------------------------------------------
table2
---------------------------------------------------------
| SubjectID | SubjectName | Number1 | Number2 |
---------------------------------------------------------
| 1 | Name1 | 6 | 4 |
| 2 | Name2 | 4 | 8 |
| 3 | Name3 | 5 | 2 |
---------------------------------------------------------
SubjectID in table1 is foreign key references table2.
I want to build a query sql that gives me the StudentID`s from table1
that didnt miss any Type 3 subject (i.e no row like this
---------------------------------------------------------
| StudentID | SubjectID | Present | Type |
---------------------------------------------------------
| 3 | 1 | no | 3 |
---------------------------------------------------------
And have completed 75 percent of type 1 (i.e
I find it like this
SELECT t1.StudentID,t1.SubjectID ,t1.Type,t2.Number1 as num
FROM table1 as t1,table2 as t2
WHERE t1.Present=yes and t2.SubjectID=t1.SubjectID
GROUP BY StudentID,SubjectID
HAVING COUNT(*)/num >= 75/100
But I cant combine the two things together.
You can combine queries by giving them aliases and joining as subqueries...
SELECT finisher.StudentID FROM
(
SELECT DISTINCT StudentID
FROM table1 t1
JOIN table2 t2 ON t2.SubjectID = t1.SubjectID
WHERE t1.Present = 'yes' AND t1.Type1 = 1
GROUP BY t1.StudentID, t2.SubjectID
HAVING COUNT(*) / t2.Number2 >= 0.75
) finisher
JOIN
(
SELECT DISTINCT t1.StudentID
FROM table1 t1
LEFT JOIN
(
SELECT DISTINCT StudentID
FROM table1
WHERE Type = 3 AND Present = 'no'
) missed ON missed.StudentID = t1.StudentID
WHERE t1.Type = 3
AND missed.StudentID IS NULL
) notmissed ON finisher.StudentID = notmissed.StudentID
"StudentID`s from table1 that didnt miss any Type 3"... I assume here you don't want to include students without any type 3 rows.
Seems like this is done and duste, but how about...
SELECT x.*
FROM
( SELECT t1.StudentID
, t1.SubjectID
, t1.Type
, t2.Number1 num
FROM table1 t1
JOIN table2 t2
ON t2.SubjectID=t1.SubjectID
WHERE t1.Present='yes'
GROUP
BY t1.StudentID
, t1.SubjectID
HAVING COUNT(*)/num >= 0.75
) x
LEFT
JOIN table1 y
ON y.student_id = x.student_id
AND y.subject_id = x.subject_id
AND y.type = 3
AND y.present = 'no'
WHERE y.student_id IS NULL;
I've two tables, Table1(News) and Table2(Subscribers).
Table1: id, news_title
Table2: id, news_id, user_id
Table1
id | news_title
--------------
1 | News 1
2 | News 2
3 | news 3
Table2
id | news_title_id | user_id
----------------------------
1 | 1 | 1
2 | 1 | 2
3 | 2 | 3
I need a query result like this.
news_title | subscribers
------------------------
News 1 | 2
News 2 | 1
News 3 | 0
Any help?
A simple aggregate COUNT(*) with a LEFT JOIN will do the job here. LEFT JOIN and COUNT(*) (rather than COUNT(user_id)) are used to be sure titles with zero subscribers still return a row.
SELECT
news_title,
COUNT(Table2.*) AS subscribers
FROM Table1 LEFT JOIN Table2 ON Table1.id = Table2.news_title_id
GROUP BY news.title
SELECT table1.PrimaryKey(Some ID), table2.nameOfSomething
FROM table1
INNER JOIN table2
Here is the part i don't get :
USING(id)
this ID is table1 foreign key, and table2 primary key
i dont really get it..
table1.ID values:
25 Rows:
row 1-5 = 1 , row 6-10 = 2 , row 11-15 = 3 , row 16-20 = 4 , row 21-25 = 5
table2.ID values :
5 Rows:
row 1 = 1 , row 2 = 2 , row 3 = 3 , row 4 = 4 , row 5 = 5
i test it and i get different result without it, how comes?
Note : Table1 contains interests, Table2 contains categories for these interests
feel free to ask for more information
USING specifies that a join should be performed by joining on the listed columns in both tables. That is
SELECT t1.col1,
t1.col2,
t2.col1
FROM table1 AS t1
INNER JOIN table2 AS t2
USING (col1)
is the same as
SELECT t1.col1,
t1.col2,
t2.col1
FROM table1 AS t1
INNER JOIN table2 AS t2
ON t1.col1 = t2.col1
For reference, see the MySql homepage.
USING is a equi-join and relies on attribute names for the same data element remaining the same between tables.
ON is more flexible: because it requires you to explicitly specify the attribute name in both tables, attribute names for the same data element can be the same or they can be different between the tables. Also, it is a theta-join, meaning that the join type can be any condition, including equality. As a result of this flexibility, ON is more verbose.
| Table1: | Table2: |
| id | id | table1_id |
| 1 | 1 3 |
| 2 | 2 2 |
| 3 | 3 1 |
If you join the above two tables together with USING(id) it will match rows where the id value in Table1 are the same as the id value in Table2...
SELECT * FROM table1 JOIN table2 USING(id)
| id | id table1_id |
| 1 | 1 3 |
| 2 | 2 2 |
| 3 | 3 1 |
But, the id in Table2 might have nothing to do with the id in Table1. If that's the case, you can use ON to be specific about how you match records together...
SELECT * FROM table1 JOIN table2 ON table1.id = table2.table1_id
| id | id table1_id |
| 1 | 3 1 |
| 2 | 2 2 |
| 3 | 1 3 |
If you specify nothing at all, you match every record in one table, against every record in the other table...
SELECT * FROM table1 CROSS JOIN table2
| id | id table1_id |
| 1 | 1 3 |
| 1 | 2 2 |
| 1 | 3 1 |
| 2 | 1 3 |
| 2 | 2 2 |
| 2 | 3 1 |
| 3 | 1 3 |
| 3 | 2 2 |
| 3 | 3 1 |