I wanna merge Parent&Child table using join query and add Mark table using union query so is that possible to do?
In the Parent Table:
Id int(primarykey),
Firstname varchar(50),
Email varchar(50)
In the Child Table:
Mid int(primarykey),
Mark1 int,
Mark2 int,
Id int(foreignkey)
In the Mark Table:
Uid int(primarykey),
Mark3 int,
Id int(foreignkey)
Finally the end result should be like this:
Id Firstname Mark1 Mark2 Mark3
-- --------- ----- ----- -----
2 John 59 78 89
Thanks in advance.
It seems like this just requires a fairly straightforward join between the tables?
select p.Id
,p.FirstName
,c.Mark1
,c.Mark2
,m.Mark3
from ParentTable as p
join ChildTable as c
on p.Id = c.Id
join MarkTable as m
on p.Id = m.Id;
Related
I am a beginner to Web programming and SQL. I am not used to work with tables that have a many to many relationship and I am facing a problem here due to my lack of knowledge.
Those are my tables and this is what I want to do:
Table users
ID | Users
-----------------
1 | John
2 | Mark
3 | Sophia
Table projects
ID | Projects
-----------------
1 | Generic Name nº 1
2 | Generic Name nº 2
3 | Generic Name nº 3
Table users_projects
UsersID | ProjectsID
-----------------
1 | 1
2 | 1
2 | 2
3 | 2
3 | 3
I want to select all the users where, let's say, the Projects.Id value is 1, while keeping the many to many realtionship between this two tables. How do I do that?
Desired Output
ID | Users
-----------------
1 | John
2 | Mark
SELECT t1.*
FROM users t1
JOIN users_projects t2 ON t1.id = t2.usersid
or
SELECT *
FROM users t1
WHERE EXISTS ( SELECT NULL
FROM users_projects t2
WHERE t1.id = t2.usersid )
I recommend you to rename ID columns in users and projects and assign them the same names which are used in users_projects. This will remove ambiguity and will make your structure and queries more clear.
If you want the result for Projects.Id value is 1 try:
select u.ID,u.Users
from users u
inner join users_projects p on u.ID=p.UsersID
where p.ProjectsID=1;
CREATE TABLE users (
ID int,
Users varchar(10) );
INSERT INTO users VALUES
(1,'John'),
(2,'Mark'),
(3,'Sophia');
CREATE TABLE users_projects (
UsersID int,
ProjectsID int );
INSERT INTO users_projects VALUES
(1,1),
(2,1),
(2,2),
(3,2),
(3,3);
Result:
ID Users
1 John
2 Mark
Demo: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=463a1cfac69f07d41a2caa440decdb25
You can try below, by using left join you specify on which fields to join.
You can use table aliases and prefix the fields with the alias to indicate between which table the fields are linked.
select u.Users, u.ID, p.Projects, p.ID
from Users u
left join user_project up
on u.ID = up.UsersID
left join projects p
on up.ProjectsID = p.projects
order by u.Users, p.Projects
i have 4 tabels
drinks, opskrifter, ingredients and stock
the tables consist of
drinks
drink_id
name
beskriv
glas
image
alcohol
opskrifter
drink_id
ingredient_id
quantity
ingredients
ingredient_id
name
stock
ingredient_id
name
i want a query to select drinks that can be made in opskrifter of the ingredients in stock.
i have this working, but it only returns drink_id.
select o.drink_id
from opskrifter o
left join stock s on s.ingredient_id = o.ingredient_id
group by o.drink_id
having count(*) = count(s.ingredient_id)
I want it to return:
drink_id, name, beskriv, glas, image, alcohol
somebody help my on the way :-) thx
Basically you need to bring the drink table. I would just use a join for that, while turning the aggregate query to a subquery:
select d.*, o.cnt_ingredients
from drink d
inner join (
select o.drink_id, count(*) as cnt_ingredients
from opskrifter o
left join stock s on s.ingredient_id = o.ingredient_id
group by o.drink_id
having count(*) = count(s.ingredient_id)
) o on o.drink_id = d.drink_id
Just add them to the list after SELECT and to the GROUP BY.
SELECT o.drink_id,
o.name,
o.beskriv,
o.glas,
o.image,
o.alcohol
...
GROUP BY o.drink_id,
o.name,
o.beskriv,
o.glas,
o.image,
o.alcohol
...
If drink_id is a key, it might also be enough to list only drink_id in the GROUP BY clause.
I don't think your query does work, you should calculate the number of ingredients used and the number that exist and you could use a cte then join for example
DROP TABLE IF EXISTS drinks,OPSKRIFTER,ingredients,stock;
create table drinks
(drink_id int,
name varchar(3),
beskriv int,
glas int,
image varchar(10),
alcohol int
);
create table opskrifter
(drink_id int,
ingredient_id int,
quantity int
);
create table ingredients
(ingredient_id int,
name varchar(3)
);
create table stock
(ingredient_id int,
name varchar(3)
);
insert into drinks(drink_id,name) values (1,'n1'),(2,'n2');
insert into opskrifter values (1,1,10),(1,2,10),(2,1,20);
insert into stock values(1,'i1'),(2,'I2');
with cte as
(
select o.drink_id,count(*) cnt,(Select count(*) from stock) sn
from opskrifter o
group by o.drink_id having cnt = sn
)
select drinks.drink_id,drinks.name
from cte
join drinks on drinks.drink_id = cte.drink_id;
+----------+------+
| drink_id | name |
+----------+------+
| 1 | n1 |
+----------+------+
1 row in set (0.00 sec)
I have two tables, one is called contacts and the other one is called numbers. One stores contact information and looks like this
contacts
-------------------------------------------------------
| id | fname | lname | email | address | uid | uniqid |
-------------------------------------------------------
My second table which stores phone numbers that belong to specific contact look like this
numbers
---------------------
| id | number | cid |
---------------------
The cid is the same as the uniqid on contact table, how can i get the contact row with its numbers which is on the second table through mysql?
Update
Correction to the correct answer
SELECT id ,fname ,lname ,email ,address , uid, uniqid,number
FROM contacts a
inner join (SELECT cid, GROUP_CONCAT(DISTINCT number SEPARATOR ',') number FROM numbers) b ON b.cid=a.uniqid
It was missing DISTINCT
use join
select id ,fname ,lname ,email ,address , uid, uniqid,number
from contacts a
inner join numbers b on b.cid=a.uniqid
You can use GROUP_CONCAT to get multiple numbers to one row and then when you imply the join you won't get duplicates.
select `id` ,`fname` ,`lname` ,`email` ,`address` , `uid`, `uniqid`,`number`
from `contacts` a
inner join (Select `cid`, GROUP_CONCAT(`number` seperator ',') `number` from `numbers`) b on b.cid=a.uniqid
You can map the two id's make sure you have this as table index, for faster retrieval of data.
SELECT id ,fname ,lname ,email ,address , uid, uniqid, number from contacts a, number b WHERE a.uniqid = b.cid;
Just use inner join with n.cid = c.uniqid
select c.id,c.fname,c.lname,c.email,c.address,c.uid,c.uniqid,n.number
from contacts c
inner join numbers n
on n.cid = c.uniqid
using join is the right choice here:
SELECT con.*,num.* from contacts as con inner join numbers as num on con.uniqid = num.cid
Here we are using the concept of foreign key . Here cid is foreign key of contact table on number table. we have to match primary key of contact table with the foreign key of number table. if both are match then it's show the result.
Select a.id, a.fname, a.lname, a.email, a.address,
a.uid, a.uniqid,b.number from contact a, number b where a.id=b.id;
I have a table like this:
id (PK) name teacher (FK) student (FK)
1 Ron 3 6
Both teacher and student are in another table called people.
people
id (PK) name age
3 Ali 42
6 Jon 12
I would like to perform a query to obtain the following
name teacher's name student's name
Ron Ali Jon
Is this possible? I know I can perform two separate joins, but then I am left with two rows and there is no indication of which name is the teacher and which is the student
select t.name, p1.name teacher_name, p2.name student_name
from t
left join people p1 on (t.teacher=p1.id)
left join people p2 on (t.student=p2.id)
Try this:
SELECT a.name, b.name 'teacher name', c.name 'student name'
FROM mainTablle a
LEFT JOIN people b ON a.teacher = b.id
LEFT JOIN people c ON a.student = c.id
WHERE a.id = 1;
I have two tables
people
name id
man1 456
man2 123
man3 789
notes
content id
testing 123
hello 456
I have two queries that select records from the table people if a note for that person exists.
SELECT * FROM PEOPLE WHERE ID IN (SELECT ID FROM NOTES)
name id
man1 456
man2 123
And
SELECT * FROM PEOPLE WHERE ID NOT IN (SELECT ID FROM NOTES)
name id
man3 789
I would like to create a column in the result set (not in the actual table) and set the value of the field based on whether a note exists.
name id does_note_exist
man1 456 yes
man2 123 yes
man3 789 no
What is the appropriate syntax to do this? Something like this would be what I'm looking for:
IF ID IN (SELECT ID FROM NOTES)
does_note_exist = yes
IF ID NOT IN (SELECT ID FROM NOTES)
does_note_exist = no
You need a LEFT OUTER JOIN:
SELECT
p.id,
p.name,
CASE WHEN n.id IS NULL THEN 'no' ELSE 'yes' END AS does_note_exist
FROM people p
LEFT OUTER JOIN notes n
ON n.id = p.id
GROUP BY n.id
There are a bunch of ways you might do that - I think I would use a case statement:
http://dev.mysql.com/doc/refman/5.0/en/case-statement.html