Connect 2 columns to 1 column - php

I need to connect 2 columns to 1 column, but I can't figure it out how to connect those columns.
This is how it looks like:
Table(appointment)
-------------------------------------------
| id | home | salesman | buyer | date |
| 1 | 3 | 2 | 4 | 12-6-2016|
| 2 | 1 | 1 | 3 | 15-6-2016|
| 3 | 2 | 5 | 6 | 20-6-2016|
-------------------------------------------
Table(person)
---------------------------------------------------------
| id | name | email | phonenumber | permission |
| 1 | John | John#gmail.com | 12345678 | 1 |
| 2 | Jack | Jack#gmail.com | 12345678 | 1 |
| 3 | Henk | Henk#gmail.com | 12345678 | 0 |
| 4 | Mike | Mike#gmail.com | 12345678 | 0 |
| 5 | Tom | Tom#gmail.com | 12345678 | 1 |
| 6 | Ben | Ben#gmail.com | 12345678 | 0 |
---------------------------------------------------------
Table(home)
--------------------------------------
| id | salesman | price | city |
| 1 | 2 | 123000 | London |
| 2 | 1 | 123000 | New York |
| 3 | 5 | 123000 | Paris |
--------------------------------------
This is how I want to see it on my php page:
-------------------------------------------------
| home | salesman | buyer | date |
| Home in Paris | Jack | Mike | 12-6-2016|
| Home in London | John | Henk | 15-6-2016|
| Home in New York| Tom | Ben | 20-6-2016|
-------------------------------------------------
People with permission 1 are salesman, permission 0 are buyers.
So my question is, how can I change the numbers to the correct value? Without change the number in table appointment to values in the database...
I don't know much about SQL, so could someone please come with a solution that isn't an example and works with my tables and columns?
Edit:
Table(afspraak)
-------------------------------------------
| id | huis | verkoper | koper | datum |
| 1 | 3 | 2 | 4 | 12-6-2016|
| 2 | 1 | 1 | 3 | 15-6-2016|
| 3 | 2 | 5 | 6 | 20-6-2016|
-------------------------------------------
Table(persoon)
---------------------------------------------------------
| id | naam | email | phonenumber | rechten |
| 1 | John | John#gmail.com | 12345678 | 1 |
| 2 | Jack | Jack#gmail.com | 12345678 | 1 |
| 3 | Henk | Henk#gmail.com | 12345678 | 0 |
| 4 | Mike | Mike#gmail.com | 12345678 | 0 |
| 5 | Tom | Tom#gmail.com | 12345678 | 1 |
| 6 | Ben | Ben#gmail.com | 12345678 | 0 |
---------------------------------------------------------
Table(huis)
--------------------------------------
| id | verkoper | prijs | stad |
| 1 | 2 | 123000 | London |
| 2 | 1 | 123000 | New York |
| 3 | 5 | 123000 | Paris |
--------------------------------------
My php code:
include('config.php');
$getAfspraak = "SELECT CONCAT('Home in ', HO.stad) AS Home,
SA.`naam` AS verkoper,
BU.`naam` AS koper,
AP.`datum`
FROM afspraak AP
INNER JOIN huis HO ON HO.id = AP.huis
INNER JOIN persoon SA ON SA.id = AP.verkoper AND SA.rechten = 1
INNER JOIN persoon BU ON BU.id = AP.koper AND BU.rechten = 0";
//Inner join for huis SELECT afspraak.huis, huis.stad FROM afspraak INNER JOIN huis ON afspraak.huis = huis.id; This query should show "huis in [stad]" it means, a home in London.
$dataAfspraak = mysqli_query($con, $getAfspraak) or die(mysqli_error($con));
<table cellspacing=1 border=0 width=100%>
<tr>
<th>Koper</th>
<th>Verkoper</th>
<th>Huis</th>
<th>Datum</th>
</tr>
<?php while($resAfspraak = mysqli_fetch_assoc($dataAfspraak)): ?>
<tr>
<td><?php echo $resAfspraak['koper']?></td> <!-- value is naam if the row has 0 at rechten -->
<td><?php echo $resAfspraak['verkoper']?></td> <!-- value is naam if the row has 1 at rechten -->
<td><?php echo $resAfspraak['huis']?></td>
<td><?php echo $resAfspraak['datum']?></td>
</tr>
<?php endwhile;?>

By the below JOIN and CONCAT query you can achieve your expected result:
SELECT CONCAT('Home in ', HO.city) AS Home,
SA.`Name` AS Salesman,
BU.`Name` AS Buyer,
AP.`Date`
FROM Appointment AP
INNER JOIN Home HO ON HO.Id = AP.Home
INNER JOIN Person SA ON SA.Id = AP.Salesman AND SA.Permission = 1
INNER JOIN Person BU ON BU.Id = AP.Buyer AND BU.Permission = 0
UPDATE:
You no need to get the huis value from separate join. The below query returns the koper, verkoper, huis and datum values.
$getAfspraak = "SELECT BU.naam AS koper, SA.naam AS verkoper, CONCAT('Huis in ', HO.stad) AS Huis, AP.datum
FROM afspraak AP
INNER JOIN huis HO ON HO.id = AP.huis
INNER JOIN persoon SA ON SA.id = AP.verkoper AND SA.rechten = 1
INNER JOIN persoon BU ON BU.id = AP.koper AND BU.rechten = 0";
$dataAfspraak = mysqli_query($con, $getAfspraak) or die(mysqli_error($con));

Related

Cross database multiple records with join in MySQL

In this case each person can be assigned to an unlimited number of companies. One of these assigned companies should always be the person's main company. As you can see in the SQL query which I have posted the ID of the person's main organization is stored in the column "main_company_id" in the "person" table.
I have database Internal on server 192.168.0.1 with Person_Company Table as given
+----+-----------+------------+------------+
| id | person_id | company_id | created_at |
+----+-----------+------------+------------+
| 1 | 1005 | 2589 | 00:00:00 |
| 2 | 1006 | 2590 | 00:00:00 |
| 3 | 1007 | 2591 | 00:00:00 |
+----+-----------+------------+------------+
Person Table is as given
+------+-------+---------+-----------------+-----------------+
| id | name | phone | main_company_id | ref_id |
+------+-------+---------+-----------------+-----------------+
| 1005 | John | 0123456 | 2590 | 273722702297768 |
| 1006 | Doe | 7894560 | 2591 | 955413080598021 |
| 1007 | Smith | 9517530 | 2589 | 164283934074454 |
+------+-------+---------+-----------------+-----------------+
Company Table is as given
+------+-----------+---------+-----------------+
| id | name | vat | ref_id |
+------+-----------+---------+-----------------+
| 2589 | Company A | 0123456 | 540603005841231 |
| 2590 | Company B | 7894560 | 725472422399397 |
| 2591 | Company C | 9517530 | 367043795528136 |
+------+-----------+---------+-----------------+
Now there is another database External on the same server 192.168.0.1 with External_Person tables as given:
+----+-----------------+-------+----------------------+--------+
| id | ref_id | name | internal_primary_key | gender |
+----+-----------------+-------+----------------------+--------+
| 1 | 273722702297768 | John | ABC123456 | male |
| 2 | 955413080598021 | Doe | BCD456789 | female |
| 3 | 164283934074454 | Smith | DEF789456 | male |
+----+-----------------+-------+----------------------+--------+
And another table on this External database is External_Company
+----+-----------------+-----------+----------------------+
| id | ref_id | name | internal_primary_key |
+----+-----------------+-----------+----------------------+
| 1 | 540603005841231 | Company A | XX4123456 |
| 2 | 725472422399397 | Company B | XX5456789 |
| 3 | 367043795528136 | Company C | XX6789456 |
+----+-----------------+-----------+----------------------+
What I want to achieve is like this result:
+----+------------+-------------+------------------+
| id | person_key | company_key | main_company_key |
+----+------------+-------------+------------------+
| 1 | ABC123456 | XX4123456 | XX5456789 |
| 2 | BCD456789 | XX5456789 | XX6789456 |
| 3 | DEF789456 | XX6789456 | XX4123456 |
+----+------------+-------------+------------------+
I have already achieved two columns through this statement:
SELECT EEP.internal_primary_key as Person_Key, EEC.internal_primary_key as Company_Key
FROM Internal.Person_Company as IPC
JOIN Internal.Person as IP on IP.id = IPC.person_id
JOIN Internal.Company as IC on IC.id = IPC.company_id
JOIN External.External_Person as EEP on IP.ref_id = EEP.ref_id
JOIN External.External_Company as EEC on IC.ref_id = EEC.ref_id
WHERE IPC.person_id = 1005;
And this has the output like this result:
+----+------------+-------------+
| id | person_key | company_key |
+----+------------+-------------+
| 1 | ABC123456 | XX4123456 |
| 2 | BCD456789 | XX5456789 |
| 3 | DEF789456 | XX6789456 |
+----+------------+-------------+
How can I get the main company's internal_primary_key of the person in this given scenario? How can I amend my this existing query to achieve the desired result which I have mentioned above?
Does this work for you? You have a lot of good work, it seems all you would need is add the EEC.internal_primary_key as main_company_key code to your select statement.
SELECT EEP.internal_primary_key as person_Key, EEC.internal_primary_key as company_Key, EEC.internal_primary_key as main_company_key
FROM Internal.Person_Company as IPC
JOIN Internal.Person as IP on IP.id = IPC.person_id
JOIN Internal.Company as IC on IC.id = IPC.company_id
JOIN External.External_Person as EEP on IP.ref_id = EEP.ref_id
JOIN External.External_Company as EEC on IC.ref_id = EEC.ref_id
WHERE IPC.person_id = 1005;
I have done this successfully after doing some research upon databases
SELECT EEP.internal_primary_key as Person_Key, EEC.internal_primary_key as Company_Key, MEEC.internal_primary_key as Main_Company_Key
FROM Internal.Person_Company as IPC
JOIN Internal.Person as IP on IP.id = IPC.person_id
JOIN Internal.Company as IC on IC.id = IPC.company_id
JOIN External.External_Person as EEP on IP.ref_id = EEP.ref_id
JOIN External.External_Company as EEC on IC.ref_id = EEC.ref_id
JOIN Internal.Company as MIC on MIC.id = IP.main_company_id
JOIN External.External_Company as MEEC on MIC.ref_id = MEEC.ref_id
WHERE IPC.person_id = 1005;
And now this results as I wanted:
+----+------------+-------------+------------------+
| id | person_key | company_key | main_company_key |
+----+------------+-------------+------------------+
| 1 | ABC123456 | XX4123456 | XX5456789 |
| 2 | BCD456789 | XX5456789 | XX6789456 |
| 3 | DEF789456 | XX6789456 | XX4123456 |
+----+------------+-------------+------------------+

Loop through every value of string

I get form db field (organisations.paths)the following strings:
/1/2/3/4
Each number is the id of organisation's name from this db table:
+----+-------------+----------+----------+----------------------+
| id | frameworkid | path | parentid | fullname |
+----+-------------+----------+----------+----------------------+
| 1 | 1 | /1 | 0 | NYC University |
| 2 | 1 | /2 | 0 | Board of directors |
| 3 | 1 | /1/2/3 | 1 | Math faculty |
| 4 | 1 | /1/2/3/4 | 3 | Statistic department |
| 5 | 1 | /1/2/3/5 | 2 | Linguist department |
+----+-------------+----------+----------+----------------------+
Then I have the description table for each organisation:
+----+----------+---------+----------------+
| id | data | fieldid | organisationid |
+----+----------+---------+----------------+
| 1 | HQ | 1 | 1 |
| 2 | advisory | 1 | 2 |
| 3 | advisory | 1 | 3 |
| 4 | bottom | 1 | 4 |
| 5 | advisory | 1 | 5 |
+----+----------+---------+----------------+
How to join the both description table and main table and loop only through organisations, which have HQ or advisory in their description? So it becomes:
NYC University, Board of directors, Math faculty (Statistic department-won't be shown, as it is with description bottom)
You need to use explode, IN and join Function of PHP and mysql.
$var = "/1/2/3/4";
$in = join(" , ", explode("/", ltrim($var, '/')));
$sql = "SELECT `dt`.`fullname` FROM `db_table` dt
LEFT JOIN `organization` o
ON `o`.`organisationid` = `dt`.`id`
WHERE `o`.`id` IN ($in) AND (`o`.`data` = 'HQ' OR `o`.`data` = 'advisory')";
Make a loop to get the names and show them as you want.

Treat data from mysql for html

I want to treat data from mysql for display in HTML, using PHP.
I have three database tables: student, course, student_x_course
student:
| idStudent | firstname | surname |
-----------------------------------
| 1 | John | Regular |
| 2 | John | Smith |
| 3 | Claire | White |
course:
| idCourse | coursename |
--------------------------
| 1 | Art |
| 2 | Music |
| 3 | Math |
| 3 | Biology |
student_x_course:
| idsc | idStudent | idCourse |
-------------------------------
| 1 | 1 | 1 |
| 2 | 1 | 2 |
| 3 | 1 | 3 |
| 4 | 2 | 1 |
| 5 | 2 | 2 |
| 6 | 2 | 4 |
| 7 | 3 | 1 |
| 8 | 3 | 2 |
| 9 | 3 | 3 |
| 10 | 3 | 4 |
And I want to create an html table which looks like this:
| Art | Music | Math | Biology |
------------------------------------------------
John Regular | x | x | x | - |
John Smith | x | x | - | x |
Claire White | x | x | x | x |
My sql query is:
SELECT s.firstname, s.surname, c.coursename FROM student AS s INNER JOIN student_x_course AS sxc ON (s.idStudent = sxc.idStudent) INNER JOIN course ON (c.idCourse = sxc.idCourse);
which gets me the following:
| John | Regular | Art |
| John | Regular | Music |
| John | Regular | Math |
| John | Smith | Art |
| John | Regular | Music |
| John | Smith | Biology |
| Claire | White | Art |
| Claire | White | Music |
| Claire | White | Math |
| Claire | White | Biology |
My question is: How can I get from many rows to lines?
Is there a better sql query or do I have to handle this in PHP Code?
Any suggestions?
You are trying to pivot your results. In mysql, you can do this with conditional aggregation:
SELECT s.idStudent, s.firstname, s.surname,
max(case when c.coursename = 'Art' then 'x' end) Art,
max(case when c.coursename = 'Music' then 'x' end) Music,
max(case when c.coursename = 'Math' then 'x' end) Math,
max(case when c.coursename = 'Biologoy' then 'x' end) Biologoy
FROM student AS s
INNER JOIN student_x_course AS sxc ON (s.idStudent = sxc.idStudent)
INNER JOIN course ON (c.idCourse = sxc.idCourse)
GROUP BY s.idStudent

Dynamically create HTML table from sql query result

how can i write the sql query to get rows from multiple MySQL table and then output it to an HTML table as the format below.
I've been trying this for hours and I'm still not getting it.
Below is the HTML structure that I am trying to create
----------------------------------------------------------------
|student_id | full_name | TERM 1 | TERM2 | TERM 3 | TERM 4 |
----------------------------------------------------------------
| 1 | John Doe | 67 | 90 | NA | NA |
| 2 | John Smi | NA | NA | NA | NA |
| 3 | Doe John | 88 | 66 | NA | NA |
| 4 | Mike Doe | 57 | 78 | NA | NA |
| 5 | Doe Mike | NA | NA | NA | NA |
| 6 | Sam Doe | NA | NA | NA | NA |
----------------------------------------------------------------
And Here are the structures for the MySQL tables
--------------------------
| student_id | full_name |
--------------------------
| 1 | John Doe |
| 2 | John Smi |
| 3 | Doe John |
| 4 | Mike Doe |
| 5 | Doe Mike |
| 6 | Sam Doe |
--------------------------
------------------------
| exam_id | exam_name |
------------------------
| 11 | TERM 1 |
| 12 | TERM 2 |
| 13 | TERM 3 |
| 14 | TERM 4 |
------------------------
----------------------------------------
| subject_id | exam_id | subject_name |
----------------------------------------
| 1 | 11 | mathematics |
| 2 | 11 | english |
| 3 | 11 | physics |
| 4 | 12 | mathematics |
| 5 | 12 | english |
| 6 | 12 | physics |
----------------------------------------
---------------------------------------------
| subject_id | marks | student_id |
---------------------------------------------
| 1 | 67 | 1 |
| 2 | 54 | 4 |
| 3 | 88 | 3 |
| 4 | 90 | 1 |
| 5 | 78 | 4 |
| 6 | 66 | 3 |
---------------------------------------------
Thanks.
This is the query you want:
select s.student_id,
s.full_name,
avg(case when te.exam_name = 'TERM 1' then sm.marks else null end) as term_1,
avg(case when te.exam_name = 'TERM 2' then sm.marks else null end) as term_2,
avg(case when te.exam_name = 'TERM 3' then sm.marks else null end) as term_3,
avg(case when te.exam_name = 'TERM 4' then sm.marks else null end) as term_4
from students s
left join subject_marks sm
on s.student_id = sm.student_id
left join subject_exams se
on sm.subject_id = se.subject_id
left join term_exams te
on se.exam_id = te.exam_id
group by s.student_id,
s.full_name
Fiddle: http://sqlfiddle.com/#!2/fd1c82/1/0
As for outputting it to an HTML table, you want to look at a PHP/MySQL tutorial.

MySQL query to show a format based on three tables

I have three tables below that shows the student records, subjects and students with subjects.
I would like to ask what is the effective SQL query to show the results below. I can show it using JOIN but not with the format below.
+------+-----------+-----------+-----+----------+-----------+--------+
| Name | Address | Telephone | Sex | Subjects | Teacher | Active |
+------+-----------+-----------+-----+----------+-----------+--------+
| John | somewhere | 12345 | M | | Teacher 1 | YES |
| John | somewhere | 12345 | M | Math | | YES |
| John | somewhere | 12345 | M | Science | | YES |
| John | somewhere | 12345 | M | English | | YES |
| Matt | somewhere | 123456 | M | | Teacher 2 | YES |
| Matt | somewhere | 23456 | M | Math | | YES |
| Matt | somewhere | 123456 | M | Science | | YES |
| Girl | somewhere | 5431 | F | | Teacher3 | YES |
| Girl | somewhere | 5431 | F | Physics | | YES |
| Girl | somewhere | 5431 | F | Math | | YES |
+------+-----------+-----------+-----+----------+-----------+--------+
select * from student_record;
+------------+------+-----------------+-----------+-----+----------+--------+
| id_student | name | address | telephone | sex | teacher | active |
+------------+------+-----------------+-----------+-----+----------+--------+
| 1 | John | Somewhere | 12345 | M | Teacher | 0 |
| 2 | Matt | Somewhere There | 12345222 | M | Teacher1 | 0 |
| 3 | Girl | Somewhere here | 3333 | F | Teacher2 | 0 |
+------------+------+-----------------+-----------+-----+----------+--------+
select * from subjects;
+------------+--------------+---------------------+
| id_subject | subject_name | subject_description |
+------------+--------------+---------------------+
| 1 | Math | Math |
| 2 | Science | Science |
| 3 | English | English |
| 4 | Physics | Physics |
+------------+--------------+---------------------+
select * from with_subjects;
+--------------------+--------------------+------------+
| id_student_subject | student_id_subject | student_id |
+--------------------+--------------------+------------+
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 4 | 1 |
| 5 | 4 | 2 |
| 6 | 3 | 2 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 2 | 3 |
| 11 | 3 | 3 |
| 12 | 4 | 3 |
+--------------------+--------------------+------------+
how about
select a.name as "Name",a.address as "Address",a.telephone as "Telephone" ,a.sex as "Sex",null as "Subject",a.teacher as "Teacher",a.active as "Active" from student_record as a
union a.name as "Name",a.address as "Address",a.telephone as "Telephone" ,a.sex as "Sex",b.subject_name as "Subject",null as "Teacher",a.active as "Active" from (student_record as a inner join with_subjects as c on a.id_student = c.student_id) inner join subjects as b on c.student_id_subject = b.id_subject
Not tested it. It wotn be in the same order as your example, but should have all of the data there

Categories