Using group_concat how to iterate each value as a column - mysql

I am using MySQL.
I want to be able to always return 1 row with 5 columns on my query. I have a student table and a committee table as follows:
Student Info Table
Student_Number Committee_Id
00000001 5
Committee Table
Committee Id Prof_Id
5 23
5 55
5 6
5 10
Using this statement:
select committee_id, group_concat(prof_id) as profs from committee_table
I get:
profs committee_id
23,55,6,10 5
I want to return:
prof1 prof2 prof3 prof4 prof5
23 55 6 10 null
Is there any way to do this simply?????

how about you try creating your committee table like this:
create table committee(committee_id datatype,prof1 datatype,prof2 datatype,prof3 datatype,prof4 datatype,prof5 datatype);
since the number of professors are static.

Related

SQL QUERY to show records of names that are recorded several times and are unique towards each other

For example, let us consider this table:
In this image consists of rows of 8 where names like Mike,Glenn,Daryl,Shane and Patricia is included with their respective ages
Id
Name
Age
1
Mike
25
2
Glenn
19
3
Glenn
19
4
Daryl
56
5
Shane
30
6
Shane
30
7
Patricia
16
Now I want to insert the type of query that will show the names without repetitions like This, not like This
EDIT: I entered the data from first picture. The request is to list the names without duplicates, as shown in the second and third picture but I will not convert them to text.
DISTINCT specifies removal of duplicate rows from the result set.
SELECT DISTINCT Name
FROM tablename
see: use DISTINCT in SELECT
You can use GROUP BY to achieve it.
SELECT * FROM your_table
GROUP BY your_table.name
ORDER BY id
With the data you gave, the result from this query will be:
id
name
age
1
Mike
25
2
Glenn
19
4
Deryl
56
5
Shane
30
7
Patricia
16

How to Update main table from second table with multiple same values in Mysql

I'm trying to update my main table by comparing it with another table that contains the same fields but have the daily values. The second table contains multiple records for same person too. (no primary key in this table)
My query executes fine but it stops when it finds the first match and doesn't scan rest of the table.
update crates as a, t23042019 as b set a.srsb=a.srsb+b.srsb, a.srsc=a.srsc+b.srsc, a.hfc=a.hfc+b.hfc, a.mix=a.mix+b.mix where a.name=b.name;
crates table
name srsb srsc hfc mix
hitesh 5 5 5 5
raman 2 3 4 1
t23042019 table
name srsb srsc hfc mix
raman 1 -2 0 1
hitesh 2 2 2 2
hitesh -5 0 0 -2
raman -1 0 0 0
Expected result after the query
crates table
name srsb srsc hfc mix
hitesh 2 7 7 5
raman 2 1 4 2
Query result
crates table
name srsb srsc hfc mix
hitesh 7 7 7 7
raman 3 1 4 2
Something like below:
UPDATE crates a
INNER JOIN (
SELECT name, SUM(srsb) AS srsb, SUM(srsc) AS srsc, SUM(hfc) AS hfc, SUM(mix) AS mix
FROM t23042019
GROUP BY name) s
ON a.name=s.name
SET a.srsb=a.srsb+s.srsb,
...

Get multiple records from two tables having primary and foreign key relationship

![enter image description here][1]In the First Table tblserialnumbersprimary i have a primary field called as serialNoId ,this field is repeating multiple times in the second table tblserialnumbers. when we are inserting multiple records with that serialNoId.
I want to get Distinct records from the first table i.e tblserialnumbersprimary and corresponding multiple records from the dependent table i.e tblserialnumbers in MySQL
First Table Fields are:
tblserialnumbersprimary (serialNoId,serialPO,serialProductNo,
SerialNumberMode,serialNoAutoPrefix,serialDateOfCreation,
serialModifiedBy,serialStatus)
Second Table Fields are:
tblserialnumbers(serialId,serialNoId,
serialNo,serialNoBatchId![enter image description here][1])
I tried With This Joins Query.But its giving multiple records of first table
select * FROM tblserialnumbersprimary
LEFT OUTER JOIN tblserialnumbers
ON (tblserialnumbersprimary. serialNoId = tblserialnumbers.serialNoId )
First Table values are:
serialNoId serialPO serialProductNo SerialNumberMode serialNoAutoPrefix serialDateOfCreation serialModifiedBy serialStatus
1 PO1 PROD121 Automatic TCS-03 2/25/2014 12:00:00 AM admin 0
2 PO2 PROD345 Automatic TCS-03 2/25/2014 12:00:00 AM admin 1
3 PO5 PROD816 Automatic 2/26/2014 12:00:00 AM admin 1
4 PO1 PROD121 Automatic GTS-03 2/26/2014 12:00:00 AM admin 1
Second Table values are:
serialId serialNoId serialNo serialNoBatchId
1 1 TCS-03-PROD121-1 batch1
2 1 TCS-03-PROD121-2
3 1 TCS-03-PROD121-3 batch3
4 1 TCS-03-PROD121-4
5 1 100
6 1 101
1 2 TCS-03-PROD345-1 batch1
2 2 TCS-03-PROD345-2
3 2 TCS-03-PROD345-3 batch3
4 2 TCS-03-PROD345-4
1 3 --1
2 3 --2
3 3 --3
4 3 --4
5 3 12
6 3 13
7 3 11
1 4 -PROD816-1 batch1
2 4 -PROD816-2 batch2
1 5 GTS-03-PROD121-1 batch1
2 5 GTS-03-PROD121-2
3 5 GTS-03-PROD121-3 batch3
4 5 GTS-03-PROD121-4
Use alias and join for record in multiple table.
For i.e. `SELECT table2.id,
table1.pid,
table1.color,
table2.pname,
table2.image
FROM tbl_productcolor table1
JOIN tbl_product table2 ON table1.pid = table2.id;`
Apply your table in this way you can get all the data.

Mysql :Exclude row that does not satisfy the condition list

So Here is My data
ID C1 C2 C3
6 Digit 2 6,8,10,12
12 Digit 3 15
15 127 Digit 2 6,7,8,9,10,11,12,13
68 140,141 Digit 11 85,86,87,88,167,168,158,159
73 1 Digit 11 85,86,87,88,169,170
76 Digit 11 85,86,87,91,164,165,166,167,168
99 Digit 11 20,27,85,86,87
106 Digit 1 1,2
111 Digit 11 85,86,87,88
112 Digit 11 85,86,87,88
135 Digit 11 85,86,87
and my condition string is (2,6,15,37,42,52,62,65,79,85,94,100,104,107,113,124,131)
Now,I want to exclude row 3,4,5 if the values 127,140,141,1 are not in the list condition. I tried Not in , but no avail. I think I might be missing something basic, but just cant get it.
It's better not to store multiple values in a column if possible. Then it's easier to do queries like this.
You cannot use "IN" or "NOT IN" because they are looking for a list of separate items. But C3 is just one item that happens to have commas in it.
Try this:
SELECT * FROM
(SELECT ID, C1, C2, CONCAT('|',REPLACE(C3,',','|'),'|') as C3 FROM `table` WHERE `C3` ) as t1
WHERE t1.C3 NOT LIKE "|127|" AND t1.C3 NOT LIKE "|140|" AND t1.C3 NOT LIKE "|141|" AND t1.C3 NOT LIKE "|1|"
You could avoid the "|" and just concat "," to the start and end.
Or you could fix your database schema so that it actually acts like a Normalized Relational Database.
Every column that contains multiple values should be separated out into its own table.
There should be no column C3 in your table above. Instead, you should have a table, some_other_data:
At this point, I see that C3=6 is related to more than one record in the main table. Therefore, you actually need a third, linking table, in addition to some_other_data. See below.
`some_other_data`
id
6
8
10
12
15
`main_table_to_some_other_data_link`
some_other_data_id | main_table_id
6 6
8 6
10 6
12 6
15 12
6 15
etc. You can see that the linking table can contain duplicates of either value. But your other two tables would have completely unique ids.
I think you're trying to solve the wrong problem.
(I'm assuming you can change your table structure. If you can't someone else will need to address your question.)
The long lists of comma-separated data are a flag that they have a one-to-many relationship with ID.
For example, make the data in C3 its own table:
ID MainID C3
================
1 6 6
2 6 8
3 6 10
4 6 12
5 12 15
6 15 6
7 15 7
8 15 8
9 15 9
10 15 10
11 15 11
12 15 12
13 15 13
// and so forth //
So ID is the primary key of the new table, MainID is the foreign key that refers to the record in your primary table, and C3 is the data in C3.
Each separate value of C3 now has its own record.
Now, you're in a position to use something like
Select * from MainTable
Inner Join NewTable
On MainTable.ID = NewTable.MainID
Where NewTable.C3 Not In (2,6,15,37,42,52,62,65,79,85,94,100,104,107,113,124,131);
If you can, pulling out the one-to-many relationships into their own tables will make things easier for you.

MySQL select unique values from a relationship table

I am currently working on a MYSQL project which has a few fairly standard many to many relationship.
I've shown a greatly simplified relationship table to aid my example.
table: book_authors
ID BOOKS AUTHORS
1 1 4
2 1 5
3 4 4
4 4 5
5 2 6
6 2 1
7 2 5
8 3 6
9 3 5
10 3 1
12 5 2
13 6 2
14 7 5
What I'm looking to achieve is to be able to select all books which have specified authors, and only get the books which match all of the supplied authors. The number of authors requested each time is also variable.
So if i'm looking for all books written by author 4 and author 5, I'll get result of books 1 and 4 only. If im looking for books written by author 5 only, i'll get book 7 only.
I don't think there's a very smooth way to do this, but if performance isn't a big concern, you can exploit MySQL's GROUP_CONCAT function, and write this:
SELECT books
FROM book_authors
WHERE books IN
( SELECT books
FROM book_authors
WHERE authors = 4
)
GROUP
BY books
HAVING GROUP_CONCAT(authors ORDER BY authors) = '4,5'
;
(The WHERE clause isn't even needed for correct results, but it makes the query less gratuitously expensive.)
If it's a one off you could do:
not very repeatable though.
select distinct
ba1.books
from
book_authors ba1
join
book_authors ba2
on ba1.id<>ba2.id and ba1.books=ba2.books
where
ba1.authors=5
and ba2.authors=4
edit: forgot part of the join