Curious as to know why this doesn't work MySQL - mysql

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;

Related

How to access a row on a table based on a value from another table?

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]

concat and group concat from a table that join with another table

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

How can I generate a unique number to a repeating row based on actual row number in mysql

I have data that has to be dumped into a mysql table. I am depending on a Bill_Number to identify the records. But for few records, the billing number is same; it is because either the customer returned it or cancelled it or something.
Now I want to generate some key for these transactions.
For example, when an ID '123456' appears first time, I want to give it a key '98765'. When the same ID 123456 comes again, I want to give it a key '98765-1' and so on.
here ist your Answer. Now you can insert every Row and field in a new Table
MariaDB [tmp]> select * from example;
+----+--------+
| id | billnr |
+----+--------+
| 1 | 6789 |
| 2 | 1234 |
| 3 | 6789 |
| 4 | 6789 |
| 5 | 2345 |
| 6 | 1234 |
+----+--------+
6 rows in set (0.00 sec)
MariaDB [tmp]> SELECT
-> #nr:=IF(#lastid = e.billnr , #nr:=#nr+1 ,0) AS recno,
-> #lastid:=billnr AS last_type,
-> concat(billnr,IF(#nr > 0, concat('-',#nr),'')) AS new_billnr,
-> e.*
-> FROM `example`e,
-> (SELECT #lastid:=0, #nr:=0) tmp
-> ORDER BY billnr;
+-------+-----------+------------+----+--------+
| recno | last_type | new_billnr | id | billnr |
+-------+-----------+------------+----+--------+
| 0 | 1234 | 1234 | 2 | 1234 |
| 1 | 1234 | 1234-1 | 6 | 1234 |
| 0 | 2345 | 2345 | 5 | 2345 |
| 0 | 6789 | 6789 | 1 | 6789 |
| 1 | 6789 | 6789-1 | 3 | 6789 |
| 2 | 6789 | 6789-2 | 4 | 6789 |
+-------+-----------+------------+----+--------+
6 rows in set (0.00 sec)
MariaDB [tmp]>

Simple query for listing elements in mysql

I have two tables::
mysql> SELECT * FROM MasterListItemtype;
+----------------------+------------------------+
| MasterListItemtypeID | MasterListItemtypeName |
+----------------------+------------------------+
| 1 | Starters |
| 2 | Main Course |
| 3 | Side Course |
| 4 | Others |
| 5 | Desert |
+----------------------+------------------------+
5 rows in set (0.00 sec)
mysql> SELECT * FROM MasterListMenuItems;
+------------------+-----------------------+-------+----------------------+
| MasterListMenuID | MasterListMenuName | Vegan | MasterListItemtypeID |
+------------------+-----------------------+-------+----------------------+
| 1 | Vada | 1 | 1 |
| 4 | Chinese Chicken Salad | 0 | 1 |
| 5 | Dosa Chicken Salad | 0 | 1 |
| 6 | Gobi Manchuri | 1 | 2 |
| 7 | Indian Salad | 1 | 3 |
| 8 | Gobi Chilli Manchuri | 1 | 2 |
| 9 | Chocolate IceCream | 1 | 5 |
| 10 | Vanilla IceCream | 1 | 5 |
+------------------+-----------------------+-------+----------------------+
8 rows in set (0.00 sec)
note here::
MasterListMenuID is primary key of MasterListMenuItems
MasterListItemtypeID is primary key of MasterListItemtype
MasterListItemtypeID is Foreign key of MasterListMenuItems
.
How to make a Mysql query so that::
I need to list the elements in MasterListMenuName of table MasterListMenuItems which are starters
try this:
select MasterListMenuName from MasterListMenuItems where MasterListItemtypeID
=
(
select MasterListItemtypeID from MasterListItemtype where MasterListItemtypeName
= 'Starters')
(OR)
select m.MasterListMenuName from MasterListMenuItems m
join MasterListItemtype n
on m.MasterListItemtypeID = n.MasterListItemtypeID
and m.MasterListMenuName = 'Starters'
SELECT * FROM MasterListMenuItems WHERE MasterListMenuItems.MasterListItemtypeID IN (SELECT MasterListItemtype.MasterListItemtypeID FROM MasterListItemtype WHERE MasterListItemtype. MasterListItemtypeName = 'Starters');
Try this
select MasterListMenuName from MasterListMenuItems mi
inner join MasterListItemtype it
on mi.MasterListItemtypeID = it.MasterListItemtypeID
where it.MasterListItemtypeName = 'Starters'
You can just use the foreign key for starter, in your case is 1.
SELECT * FROM MasterListMenuItems WHERE MasterListItemtypeID = 1

MySQL select from multiple table with conditions

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