PHP MySQL combine cells from different row with same value - php

I have a database that has id numbers, names, class period, and teacher name. Each row includes a student's id, name, period, and teacher which means that there are multiple rows for each student. That's not my problem. My problem is that some classes have two teachers listed...which means there are two separate rows for 1 class period, one for each teacher. Unfortunately I have no control over the formatting of the data since it's exported from a system that doesn't have a lot of formatting options.
Here is an example, notice how rows 23 and 24 are for the same class period and student, but the only thing different is the teacher names.
pk id last first period teacher
14 12345 Smith John 3 HARRIS
15 12345 Smith John 8 LEAL
17 12345 Smith John 1 HOUSTON
23 56789 Doe Jane 8 MERCER
24 56789 Doe Jane 8 RUIZ
25 56789 Doe Jane 3 BECK
26 56789 Doe Jane 1 STEED
I would like to combine the two rows with the same period number and student name into one row. All the information would remain the same except the two different teacher's names would be combined into something like "Mercer & Ruiz." Ideally the final result would look something like,
24 56789 Doe Jane 8 MERCER & RUIZ
Is this possible using PHP and/or MySQL? I'm not looking for anyone to write the entire code or anything. I just can't seem to think of a way to accomplish it. I'd be happy with any direction/indication of a way to go about this.
As always, thanks for your time and help.

SELECT MAX(pk),
ID,
`last`,
`first`,
GROUP_CONCAT(teacher SEPARATOR ' & ') teachers
FROM tableName
// WHERE clause here...
GROUP BY `last`, `first`, `Period`
// ORDER BY clause here....
SQLFiddle Demo
SOOURCE
GROUP_CONCAT()

SELECT pk,id,last,first,period GROUP_CONCAT(teacher)
FROM table
GROUP BY id, first,last,period
Should do what you need.

Related

Inserting values into database that depends on other inputs in a PHP form

I have two tables say Companies and Employees. The table company has three columns, that are ID, Name, Emp_set_id and the table Employees has Emp_id, Company and several other columns.
The Emp_set_id column in Companies table has a value format for each company. Something like below.
**ID** | **Name** | **Emp_set_id**
01 | ABC | ABCxxxxyy
02 | QWE | QWExxxxyy
Details:
First three characters in Emp_set_id is the company name.
The next four characters xxxx must be replaced with current year.
The next two characterss yy must be replaced with two digits and it is the number of that employee joining that company and it should auto increment.
When a user fills the form, the details are stored in the Employees table. This part has been done, except for one column, that is Emp_id. Now, while filling the form, if the user selects company as ABC in dropdown box at the form, the value entered in Emp_id should be like ABC202001, ABC202002,... if the company selected is ABC and QWE202001, QWE202002,... if the company selected is QWE. The table should look something like below.
**Emp_id** | **Company** | **Emp_name**
ABC202001 | ABC | John
QWE202001 | QWE | Doe
ABC202002 | ABC | Jane
ABC202003 | ABC | Paul
QWE202002 | QWE | Laura
The part where entering the value into Emp_id in the desired format is where I need help.
I have created multiple PHP files for this project. The HTML code segment to display the form is in Employee_tpl.php file. The php code to execute SQL queries for fetching data for the dropdown boxes and inserting values into the table are stored in Employee.php file.
I'm new for both PHP and stack overflow. So, if my question is not clear enough, please imply the content that needs to be elaborated. I have not attached the code here as you may get the idea of my requirement through my words and also due to me not sure of which part of the code to attach.
Thank you.
So you need to get the count from the Employees table added for a compagny this year.
A SQL query will do it :
SELECT COUNT(Emp_set_id) FROM Employees WHERE Emp_set_id LIKE "QWE2020%";
Php sample (not tested) :
$dbh = new PDO("_________");
$sth = $dbh->prepare('SELECT COUNT(Emp_set_id) as countEmployees FROM Employees WHERE Emp_set_id LIKE :clause;');
$sth->execute([':clause' => $compagnyChar . date('Y').'%']);
$countEmployees = $sth->fetchColumn();
$employeeId = $compagnyChar . date('Y') . str_pad($countEmployees +1, 2, '0', STR_PAD_LEFT);

order by specific word count in mysql

I need to order the results based on the repeatness of the word using mysql.
Here is my sample table
id Name keywords Description
1 John John, USA John is good boy. John, John
2 Alex Alex, John Alex is a friend of john.
3 Rocky John Rocky
4 John John,John John, John, John, John, John
Will take an "John" as example. In first row "John" is repeated 5 times, 2 times repeated in second row, 1 time repeated in 3rd row and 8 times repeated in 4th row. I need to show the results based on the count descending.
Select * From table Where name like '%John%' OR keywords like '%John%' OR Description like '%John%'
So it will show in below order
id Name keywords Description
4 John John,John John, John, John, John, John
1 John John, USA John is good boy. John, John
2 Alex Alex, John Alex is a friend of john.
3 Rocky John Rocky
This will do the trick:
SELECT id,Name,keywords,Description,
ROUND (
(
LENGTH(CONCAT(Name,keywords,Description))
-LENGTH(REPLACE( CONCAT(Name,keywords,Description), "John", "") )
) / LENGTH("John")
) AS count
FROM tbl ORDER BY `count` desc
see here: Demo
Update
If you want to look for multiple (different) words per record you
you should use a user-defined function (UDF) like
CREATE function wcnt(wrd varchar(32), str varchar(1000)) returns int
RETURN ROUND ((LENGTH(CONCAT(str))-LENGTH(REPLACE( CONCAT(str),wrd,"")))/LENGTH(wrd));
see here: function-demo
or here for a combination of the first query with the UDF
SELECT * FROM T
ORDER BY FIND_IN_SET('John',Description) DESC
FIDDLE

Php mysql routine for calculating in one table and place results in another table

I need a php routine for my mysql database. I have made an example to illustrate the problem:
Lets say I have a table that registrates customers and how much money they spend. A customer can have more registrations:
Table1:
Name - Amount
Jane - 3
Mark - 4
Sara - 5
Jane - 5
Jane - 6
Sara - 2
I want a routine that goes trough Table1, and finds how much each person has spend. I want the result in Table2, like this:
Table2:
Jane - 14
Mark - 4
Sara - 7
Do you have a solution to this?
insert into table2 select name, sum(amount) from table1 group by name;
SELECT Name, SUM(Amount) FROM Table1 GROUP BY Name

MySQL query problematic

I'm using PHP/MySQL and I have to create a new table from an existing one. Here's the problematic:
Table1:
photo_id email name_to creation_date link
1 foo1#bar.com paul 2012-11-21 link1.com
2 foo2#bar.com mark 2012-11-22 link2.com
3 foo1#bar.com alex 2012-11-23 link3.com
4 foo1#bar.com saul 2012-11-25 link4.com
5 foo1#bar.com john 2012-11-26 link5.com
6 foo2#bar.com math 2012-11-27 link6.com
7 foo3#bar.com fred 2012-11-28 link7.com
In Table1 the email is not unique, it can be repeated several times. Each link is different. With this data, I have to create a new table in which the email is unique with maximum of 3 links if there's more entries of one email (if so I need the data of the 3 latest entries).
So, in this case, Table2 would be like:
email link1 creation_date1 name_to1 link2 creation_date2 name_to2 link3 creation_date3 name_to3
foo1#bar.com link5.com 2012-11-26 john link4.com 2012-11-25 saul link3.com 2012-11-23 alex
foo2#bar.com link6.com 2012-11-27 math link2.com 2012-11-22 mark
foo3#bar.com link7.com 2012-11-28 fred
I know the GROUP_CONCAT feature but it's not really what I need here since the links would all be in the same column. Is it better to make a SELECT * FROM table1 and process the result into PHP arrays and after that create Table2 or a unique MySQL query would do the trick? Or create multiple MySQL tables?
Table1 is over 10 millions rows.
Any advice would be appreciate.
Thanks.
1) select all unique emails.
2) For each email, take the first 3 rows with that email ordered by creation_date descending.
3) Use that data to insert into new table.
Whatchu think?

MySQL/PHP adding new column in PHP output after new value in column in MySQL

apologize if there is already answer for that, but as a newbie to I'm not able to recognize one.
I have a trouble with outputing data from MySQL.
Imagine a table "visits" in MySQL:
1. name------surname------date------visitTime
2. John White 20111017 13:00
3. John White 20111017 17:00
4. John White 20111017 19:00
5. John White 20111018 07:00
6. Jack Black 20111017 09:00
7. Jack Black 20111018 16:00
I would like to have number of visits sumarized by date and printed out on the php page like this
1. name surname 20111017 20111018 ----> other days
2. John White 3 1
3. Jack Black 1 1
the query I'm using so far is
$query = "select name,surname,count(*) as visitCount
from visits group by name,surname,date";
thanks a lot
I don't think you want to do it like that. Like KA_lin said this could get very very ugly as you get more and more different visit days.
A cleaner solution would be to group the data with this query.
"select name, surname, date, count(date)
from visits
group by name, surname, date;"
name surname date visitCount
Jack Black 20111017 1
Jack Black 20111018 1
John White 20111017 3
John White 20111018 1
Use trigger on insert(your table) (increment the value of a colomn visit_number if new, else insert), you can have a log for each visit(current table) and another table with the number, so that you won`t have to query by COUNT(), imagine 1 milion visits...

Categories