I have two tables:
Table1 name object Table2 name_old name_corr
------|-----| ---------|-----------
John | A | John | John
Ben | B | Ben | Ben
Jon | B | Jon | John
Be n | B | Be n | Ben
Peter | B | Peter | Peter
Petera| C | Petera | Peter
In my Example I have three persons, in Table1 there are some typing errors, so Table2 assigns every name to the correct name.
Now I want for every Person (John, Ben, Peter) their distinct objects.
This would be the outcome:
John A
B
Ben B
Peter B
C
This was my try, but I get an error:
Select b.name_corr, distinct(a.object) from Table1 as a join Table2 as b on (a.name=b.name_old) group by b.name_corr
Without the grouping, meaning if I select a specific name via 'where' my query works.
distinct isn't a function. It is a qualifier on select:
Select distinct b.name_corr, a.object
from Table1 a join
Table2 b
on a.name = b.name_old;
use group_concat:
Select b.name_corr, group_concat(distinct a.object) from Table1 as a join Table2 as b on (a.name=b.name_old) group by b.name_corr;
I figured out a solution for my problem. -> Double 'group by'. So simple..
Thanks for the help anyway.
Related
Table_Teacher
id | name
-----------------
1 | Kevin
2 | Alex
3 | jax
4 | Albert
Table_Supervisor
id | id_teacher
-----------------
1 | 1
2 | 3
I want to display 2 data (Alex & Albert) in table_teacher
with queries
SELECT A.name FROM table_teacher A,tbl_supervisor B WHERE B.id_teacher != A.id;
why is this not working ?
There are various ways.
A simple one is to use a subquery to get all teachers that are not contained in the subquery of supervisor
SELECT A.name FROM table_teacher a where a.id not in
(select b.id_teacher from table_supervisor b WHERE b.id_teacher = a.id);
You could use LEFT JOIN, which might perform better than subqueries.
select t.name
from Teacher t
left join Supervisor s on t.id=s.id_teacher
where s.id_teacher is null;
Above query will get only the values which are on Teacher table but not on Supervisor table
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=98ae9aa420456e69bae05f00ec34eb43
So I am new to mySQl and am trying to learn it. I want to get the name of Johns chef. So John Chefnr is 100. I want to make a query where I get chefname Frank as output. How do I do this? Help is appreciated!
+-----+-------+--------+
| eNr | name | chefNr |
+-----+-------+--------+
| 120 | John | 100 |
+-----+-------+--------+
| 100 | Frank | 200 |
+-----+-------+--------+
This query:
select chefnr from tablename where name = 'John'
will return 100 in the column chefnr.
You can use it like this:
select name from tablename
where enr = (select chefnr from tablename where name = 'John')
to get the name of John's chef.
See the demo.
Result:
| name |
| ----- |
| Frank |
You need a join that involve the same table two time ..
select a.name as chefname
from my_table a
inner join my_table b on a.eNr = b.cheNr
where b.name is 'John'
or using chefNr
select a.name as chefname
from my_table a
inner join my_table b on a.eNr = b.cheNr
where b.chefNr = 100
I've been trying to produce a query that will search table1, and then CONCAT the all the values of table2.column1 where table1.id = table2.owner
People
name | id
-------------
tim | 1
jill | 2
john | 3
Dogs
name | owner
--------------
a | 1
b | 1
c | 2
d | 2
Using the following table i need a query that would output
name | dogs
-----------
tim | a, b
jill | c, d
john | NULL (or a blank text or just so long as the name john still shows)
I have spent a few hours and really cant do it. Some form of mash between OUTER JOIN, and group_concat(), i think. But i didnt really get close to my answer.
Thank you for all help!
You will want to use GROUP_CONCAT and a GROUP BY
SELECT p.name, GROUP_CONCAT(d.name)
FROM people p
LEFT JOIN dogs d
ON p.id = d.owner
GROUP BY p.name
see SQL Fiddle with Demo
I guess you looking for GROUP_CONCAT in MySQL.
SELECT a.name, GROUP_CONCAT(b.name) dogsName
FROM People a
LEFT JOIN Dogs b
ON a.id = b.owner
GROUP BY a.name
SQLFiddle Demo
Table: A Table: B Table: C
------------ ---------------- -------------
P_id | G_id P_id | Name G_id | Title
------------ ---------------- -------------
1 | 1 1 | john 1 | php
2 | 1 2 | jack 2 | sql
3 | 2 3 | sam
Now I am quering like:
Select B.name, C.title
from B inner join A on...
inner join c on...
If we input john here then it will display like this:
john php.
But I want to display it like:
john jack php.
Because G_id of john and jack is same.
How can i do this?
Pseudo code (similar to mysql):
SELECT B.name, C.title
FROM B
INNER JOIN A ON A.P_id = B.P_id
INNER JOIN C ON A.G_id = C.G_id
WHERE A.G_id = (
SELECT A.G_id
FROM B
INNER JOIN A ON A.P_id = B.P_id
WHERE B.Name LIKE '%John%' LIMIT 1
);
EDIT:
This will make your results searchable by name, use GROUP_CONCAT and GROUP BY as suggested by Everton Agner to correctly format the results.
You need an Aggregation Funcion to work with this kind of grouping. I'm not fluent at MySQL, but try something pretty much like this, using the group_concat() function:
select
group_concat(b.Name),
c.Title
from
A a
join B b on b.P_id = a.P_id
join C c on c.G_id = a.G_id
group by
c.Title
Hopefully it'll show you "john,jack"
Check the docs about Aggregation functions here: http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
-- EDIT
Just tested it, it gave me the following output:
+----------------------+-------+
| group_concat(b.Name) | Title |
+----------------------+-------+
| john,jack | php |
| sam | sql |
+----------------------+-------+
I hope is that what you want :)
-- EDIT (the last one)
Now I think I understood what you want, just add having group_concat(b.Name) like '%john%' and it'll give you only the groups that john is included... The better choice would be an array contains function, but I haven't found it.
+----------------------+-------+
| group_concat(b.Name) | Title |
+----------------------+-------+
| john,jack | php |
+----------------------+-------+
I am trying to do the following in ruby on rails, but even if I can get an answer in mysql that would be great.
I have tables Student and Courses. Student -> Courses is one-to-many relationship.
Student Course
|--------------| |--------------------------------------|
|id| name | |id | course_name | grade | student_id |
|---------- | |--------------------------------------|
|S1| student 1 | |C1 | Course1 | A | S1 |
|S2| student 2 | |C2 | Course2 | C | S1 |
|---------- | |C3 | Course1 | A | S2 |
|C4 | Course2 | B | S2 |
|--------------------------------------|
select * from Student
where id NOT IN (select student_id from Course where grade = 'C')
I want to achieve same result using single SQL JOIN statement or using activerecord.
SELECT * FROM Student s
LEFT JOIN Course c ON s.id=c.student_id AND c.grade = 'C'
WHERE c.student_id IS NULL;
Or join to your subquery
SELECT * FROM Student s
LEFT JOIN (SELECT student_id FROM Course WHERE grade = 'C') c
WHERE c.student_id IS NULL;
Or use exists
SELECT * FROM Student s
WHERE NOT EXISTS (SELECT NULL FROM Course WHERE grade = 'C' AND student_id=s.id);
I'm afraid I can't test this at the moment and I have a suspicion that these queries may not help you. I have no experience with ActiveRecord. Let me know in the comments.
oracle:
select distinct s.* from student s left join course c on c.student_id=s.id where c.grade!='c'
Student.all(:select=>"distinct student.*",:joins=>"left join course on course.student_id=student.id",:conditions=>"course.grade!='c'")