How to use concat and group concat from a table that join with another table.
The schema looked like this :
FIRST TABLE :
MariaDB [ittresnamuda]> select * from tb_tipe_request;
+---------+------------+
| id_tipe | nama_tipe |
+---------+------------+
| 1 | Perbaikan |
| 2 | Permintaan |
+---------+------------+
2 rows in set (0.00 sec)
SECOND TABLE
MariaDB [ittresnamuda]> select a.ID_REQUEST, a.CATATAN from tb_requestfix a;
+------------+---------------------------------+
| ID_REQUEST | CATATAN |
+------------+---------------------------------+
| 3 | Akan kami cek jaringan tersebut |
| 4 | Iya, go ahead. Appproved |
| 5 | Sudah di refill |
| 28 | Saja |
+------------+---------------------------------+
4 rows in set (0.00 sec)
THIRD TABLE
MariaDB [ittresnamuda]> select * from tb_link_tipe_request;
+----+------------+---------+
| id | id_request | id_tipe |
+----+------------+---------+
| 8 | 4 | 1 |
| 9 | 4 | 2 |
| 11 | 3 | 1 |
| 12 | 5 | 2 |
| 40 | 28 | 1 |
+----+------------+---------+
5 rows in set (0.00 sec)
I already use join, concat, and group_concat, but still no result. I need to select the table like this :
+------------+---------------------------------+------------------------+
| ID_REQUEST | CATATAN | TIPE_REQUEST |
+------------+---------------------------------+------------------------+
| 3 | Akan kami cek jaringan tersebut | Perbaikan |
| 4 | Iya, go ahead. Appproved | Perbaikan / Permintaan |
| 5 | Sudah di refill | Permintaan |
| 28 | Saja | Perbaikan |
+------------+---------------------------------+------------------------+
For the help, thanks a lot.
You can join all the tables together, and then use GROUP_CONCAT like this:
select a.ID_REQUEST, a.CATATAN ,group_concat(t.nama_tipe separator ',') as tipe_request
from tb_requestfix a
INNER JOIN tb_link_tipe_request at
ON(a.id_request = at.id_request)
INNER JOIN tb_tipe_request t
ON(t.id_tipe = at.id_tipe)
GROUP BY a.id_request
Related
What I'm trying to do is get every teacher name involved with the class_id provided. And it does nothing, very strange.The tables the query is connected to:
+----+------------+----------+
| id | teacher_id | class_id |
+----+------------+----------+
| 1 | 1 | 2 |
| 6 | 1 | 4 |
| 10 | 2 | 3 |
| 5 | 2 | 5 |
| 4 | 3 | 1 |
| 7 | 3 | 3 |
| 9 | 4 | 2 |
| 2 | 4 | 4 |
| 3 | 5 | 1 |
| 8 | 5 | 5 |
+----+------------+----------+
10 rows in set (0,00 sec)
mysql> select * from classes;
+----------+------------+------------------------+
| id_class | class_name | responsible_teacher_id |
+----------+------------+------------------------+
| 1 | Geography | 2 |
| 2 | Reading | 1 |
| 3 | Science | 3 |
| 4 | History | 5 |
| 5 | Arithmetic | 4 |
+----------+------------+------------------------+
5 rows in set (0,00 sec)
mysql> select * from teachers;
+----+------------+-------------+-----------+------------+------------+-------------+----------------------------------+
| id | first_name | middle_name | last_name | birthdate | subject_id | current_age | md5 |
+----+------------+-------------+-----------+------------+------------+-------------+----------------------------------+
| 1 | Oliver | Weed | Saxon | 1983-01-03 | 2 | 38 | 4bf51ce48af83dba7c8390a7222db991 |
| 2 | Dexter | Tristan | Morgan | 1980-12-11 | 5 | 40 | be96d6213828fd09538d8b1357907d54 |
| 3 | Deborah | Ocean | Morgan | 1985-06-10 | 2 | 35 | ba6869ece0a74ca9987b83b4f840405b |
| 4 | George | Kai | Smith | 1965-01-03 | 6 | 56 | ccec54863dbb6b63e55d495341fd9a63 |
| 5 | Chi | Kai | Chian | 1979-10-03 | 3 | 41 | 2291ce528bf05eff905af1eecd9bc53b |
+----+------------+-------------+-----------+------------+------------+-------------+----------------------------------+
5 rows in set (0,00 sec)
And the query:
select first_name
, middle_name
, last_name
FROM teachers_seth t
JOIN classes_sethputz c
ON c.responsible_teacher_id = t.id
JOIN teachers_classes_sethputz t_c
ON t_c.class_id = c.id_class
AND t_c.teacher_id = t.id
WHERE c.id_class = 1;
And what the query returned each time I ran it with a different class_id:
c.id_class = 1;
Empty set (0,00 sec)
c.id_class = 2;
+------------+-------------+-----------+
| first_name | middle_name | last_name |
+------------+-------------+-----------+
| Oliver | Weed | Saxon |
+------------+-------------+-----------+
1 row in set (0,00 sec)
c.id_class = 3;
+------------+-------------+-----------+
| first_name | middle_name | last_name |
+------------+-------------+-----------+
| Deborah | Ocean | Morgan |
+------------+-------------+-----------+
1 row in set (0,00 sec)
c.id_class = 4;
Empty set (0,00 sec)
c.id_class = '5';
Empty set (0,00 sec)
Thanks. This is really strange for me.
You have too much joining going on for your question, you have 3 tables, teachers, classes and a matrix that links them many-many.
You are joining the classes table directly to the teachers table with the responsible_teacher_id column, this should not be there if you want the teachers involved with the classes. What you are doing is saying if the teachers are the responsible teacher AND the teacher is involved in the class and that is rarely the case so you get very few hits.
So to solve it, start with the class table (classes_sethputz), as that is what you appear to be doing and then add in the matrix table (teachers_classes_sethputz) by joining it then add in the teachers table (teachers_seth) by joining it to the matrix table (teachers_classes_sethputz).
SELECT first_name
, middle_name
, last_name
FROM classes_sethputz c
JOIN teachers_classes_sethputz t_c
ON t_c.class_id = c.id_class
JOIN teachers_seth t
ON t.id = t_c.teacher_id
WHERE c.id_class = 5;
I am doing a mini project using MySQL. I came with the following problem:
I created 2 tables, student and book with 6 and 5 columns respectively.
mysql> select * from book;
+--------+------+------------+---------+------+
| bookid | Name | Authorname | edition | cost |
+--------+------+------------+---------+------+
| cc12 | dbms | guna | 5 | 500 |
| cc34 | CA | fasil | 5 | 600 |
| cs113 | OS | rohan | 3 | 300 |
| cs12 | AI | ganesh | 2 | 1000 |
| cs343 | c# | jackesh | 4 | 300 |
+--------+------+------------+---------+------+
5 rows in set (0.00 sec)
mysql> select * from studentbook;
+-----+--------+-----------+
| Sno | bookid | Studid |
+-----+--------+-----------+
| 1 | cc12 | 14vec1088 |
| 2 | cs113 | 14vec1099 |
| 3 | cc34 | 14vec1132 |
| 4 | cs343 | 14vec2011 |
| 5 | cs12 | 14vec100 |
+-----+--------+-----------+
5 rows in set (0.00 sec)
Now, when I enter any of the studid mentioned in the table studentbook (This is performed by PHP in the backend) it should display the details of book associated with the respective studid from the table book.
How can I perform the above using MySQL Query?
This might be work.
SELECT * FROM book
WHERE bookid IN
(SELECT bookid FROM studentbook
WHERE studid = "[Id of which you want book]");
This should get you what you need.
SELECT b.*
FROM book b
INNER JOIN studentbook sb on b.bookid = sb.bookid
WHERE sb.Studid = [your id]
I Have a MySQL DataBase which contains two tables linked with a foreign key:
MatPr(id, Designation, threshold_stock)
And
MvtStock_MatPr(id, Qte_Mvt, Qte_Tot,idMatPr)
What I want is to get the last Qte_tot, Designation,threshold_stock where threshold_stock>qte_tot
I tried this code and did not work:
SELECT MvtStock_MatPr.id,idMatPr, Nom, threshold_stock, Qte_Tot
FROM MvtStock_MatPr, MatPr
WHERE MatPr.id=MvtStock_MatPr.idMatPr AND threshold_stock>Qte_Tot
Here is Sample Data set:
mysql> SELECT MvtStock_MatPr.id,idMatPr, Designation, threshold_stock, Qte_Tot FROM MvtStock_MatPr, MatPr WHERE MatPr.id=MvtStock_MatPr.idMatPr AND threshold_stock>Qte_Tot ORDER
BY id,idMatPr;
| id | idMatPr | Dsignation| threshold_stock| Qte_Tot |
| 1 | 1 | bakra | 120 | 10 |
| 2 | 2 | zipper | 130 | 20 |
| 3 | 1 | bakra | 120 | 30 |
| 7 | 2 | zipper | 130 | 50 |
4 rows in set (0.00 sec)
mysql> SELECT * FROM MatPr;
| id | Designation| threshold_stock|
| 1 | bakra | 120 |
| 2 | zipper | 130 |
2 rows in set (0.00 sec)
mysql> SELECT * FROM MvtStock_MatPr;
| id | DateMvt | Qte_Mvt | Qte_Tot | idMatPr |
| 1 | 2016-01-01 | 10 | 10 | 1 |
| 2 | 2016-02-02 | 20 | 20 | 2 |
| 3 | 2016-03-03 | 20 | 30 | 1 |
| 4 | 2016-03-03 | 100 | 130 | 1 |
| 5 | 2016-03-03 | 50 | 180 | 1 |
| 6 | 2016-03-03 | 20 | 200 | 1 |
| 7 | 2016-03-05 | 30 | 50 | 2 |
7 rows in set (0.00 sec)
What I would like to get is:
| id | idMatPr | Dsignation| threshold_stock| Qte_Tot
| 3 | 1 | bakra | 120 | 30 |
| 7 | 2 | zipper | 130 | 50 |
+----+---------+--------+-------+---------+
Thanks for your contributions
Use the below query
Select m1.id,m1.idMatpr,m.threshold_stock,m.Qte_Tot
From MvtStock_Matpr m1 join Matpr m
On m1.idMatPr=m.id and m.threshold_stock>m1.Qte_Tot
I'm not sure that I completely follow what your problem is: is it selecting just one row from a result set? If so, does a subquery fix your problem?
SELECT *
FROM MatPr
WHERE ID = (
SELECT idMatPr
FROM MvtStock_MatPr, MatPr
WHERE MatPr.id = MvtStock_MatPr.idMatPr
AND threshold_stock > Qte_Tot
LIMIT 1
)
If you want to select just the rows from MatPr, does this work for you?
SELECT MatPr.*
FROM MvtStock_MatPr, MatPr
WHERE MatPr.id = MvtStock_MatPr.idMatPr
AND threshold_stock > Qte_Tot
http://sqlfiddle.com/#!9/d4ef50/2
Try this query:
select * from
( SELECT MvtStock_MatPr.id,idMatPr,Designation, threshold_stock, Qte_Tot
FROM MvtStock_MatPr join MatPr
on MatPr.id=MvtStock_MatPr.idMatPr
where threshold_stock>Qte_Tot
order by DateMvt desc
) T
group by T.idMatPr
I want to find the average of the following data via mysql query (assume these are 719 rows).
| 1 |
| 3 |
| 1 |
| 2 |
| 2 |
| 1 |
| 1 |
| 2 |
| 1 |
| 1 |
| 2 |
| 1 |
| 2 |
| 1 |
| 2 |
| 1 |
| 1 |
+----------+
719 rows in set (2.43 sec)
SELECT COUNT(*) FROM osdial_agent_log WHERE DATE(event_time)='2015-11-01' GROUP BY lead_id;
I ran this query to get that data
Can someone help me to find the average for the above data.
Use
SELECT AVG(total)
FROM (SELECT COUNT(*) AS total
FROM osdial_agent_log
WHERE DATE(event_time)='2015-11-01'
GROUP BY lead_id) t
I have 4 tables:
ARTICOLE
BAR
BUCATARIE
MAGAZIE
mysql> select * from ARTICOLE;
| OID | ART |
| 1 | TEST |
| 2 | TESTQ |
| 3 | MYART |
| 4 | MYARTBUC |
4 rows in set (0.00 sec)
mysql> select * from BAR;
| OID | ART | CANT |
| 1 | TEST | 3.00000 |
| 2 | TESTQ | 1.00000 |
| 3 | MYART | 20.00000 |
3 rows in set (0.00 sec)
mysql> select * from BUCATARIE;
| OID | ART | CANT |
| 1 | TEST | 5.00000 |
| 2 | MYARTBUC | 10.00000 |
2 rows in set (0.00 sec)
mysql> select * from MAGAZIE;
Empty set (0.00 sec)
the below query
mysql> select a.ART,sum(bar.CANT),sum(buc.CANT),sum(mag.CANT) from ARTICOLE a,BUCATARIE buc,BAR bar,MAGAZIE mag where a.ART=bar.ART and a.ART=bar.ART and a.ART=mag.ART group by a.ART;
return:
Empty set (0.00 sec)
how must be query to return:
| ART | sum(bar.CANT) | sum(buc.CANT) | sum(mag.CANT) |
TEST | 3.00000 | 5.00000 | NULL |
TESTQ | 1.00000 | NULL | NULL |
MYART | 20.00000 | NULL | NULL|
MYARTBUC | NULL | 10.00000 | NULL |
????
Any help appreciated.
You need to make use of LEFT JOIN to include results from other tables without filtering out records that don't match:
select a.ART,sum(bar.CANT),sum(buc.CANT),sum(mag.CANT)
from ARTICOLE a
left join BUCATARIE buc on buc.ART = a.ART
left join BAR bar on bar.ART = a.ART
left join MAGAZIE mag on mag.ART = a.ART
group by a.ART;
Sample results:
ART SUM(BAR.CANT) SUM(BUC.CANT) SUM(MAG.CANT)
MYART 20
MYARTBUC 10
TEST 3 5
TESTQ 1
Demo: http://www.sqlfiddle.com/#!2/6efb2/2