Get staff name from table STAFF - mysqli

can anyone provide me with solution for my coding. For your information, I made a query from my answer table which only consist id, staff_id, dept_name, question_id, ans, evaluator, and year. Below is my code:-
// Make a mysqli Connection
$connect = new mysqli('localhost', 'root', '', 'cpsdatabase');
//Mean by staff Id
$dept_name = $_GET['dept_name'];
$query = "SELECT staff_id,dept_name, AVG(ans)
FROM hodanswer WHERE dept_name='$dept_name'
group by staff_id";
$result=mysqli_query($connect, $query);
// Print out result
while($row = mysqli_fetch_array($result))
{
echo "The mean of staff id = &nbsp". $row['staff_id']."&nbsp&nbsp from department &nbsp".$row['dept_name']." &nbsp &nbsp &nbsp is &nbsp &nbsp". $row['AVG(ans)'];
echo "<br />";
}
I want to find mean and I did get the result. My problem is I want to retrieve the staff name based on staff id but staff name does not include in answer table. Staff name provided in staff table. How can I retrieve staff_name from table STAFF and display result based on code above. Please help me.

If I'm understanding your tables correctly then this should work. This will perform an inner join between tables STAFF and hodanswer and will display all staff id and staff name from table STAFF, respectively, where the staff id is equal to the staff_id(s) that are present in table hodanswer.
SELECT a.id, a.staff_name FROM STAFF a INNER JOIN hodanswer b ON a.id = b.staff_id;
Google up SQL INNER JOIN

Related

T-SQL Question for Getting One Customer Type When There Can be More Than One Value

We have an organization that can have more than one customer type basically. However, what a user wants to see is either the partner or direct type (customer type is either Direct, Partner1, Partner2, or Partner3 but can be direct plus a partner value but only can be one of the partner values). So if a customer is both (ex: Direct and Partner1) they just want the type that is a partner (ex: Partner1). So I tried splitting out partners only into one temp table from a few tables joining together different org data. I have the same query without any limit pulling into a different temp table. Then I calculate count and put that into a temp table. Then I tried gathering data from all the temp tables. That is where I run into trouble and lose some of the customers where the type is direct (I have a image link below for a directcustomer and a customer who is both). I have been out of SQL for a bit so this one is throwing me...I figure the issue is the fact that I have a case statement referencing a table that a direct customer will not exist in (#WLPO). However I am not sure how to achieve pulling in these customers while also only selecting which partner type it is for a customer that has a partner and is also direct. FYI using MSSMS for querying.
If OBJECT_ID('tempdb..#WLPO') IS NOT NULL
DROP TABLE #WLPO
IF OBJECT_ID('tempdb..#org') IS NOT NULL
DROP TABLE #org
IF OBJECT_ID('tempdb..#OrgCount') IS NOT NULL
DROP TABLE #OrgCount
IF OBJECT_ID('tempdb..#cc') IS NOT NULL
DROP TABLE #cc
Select
o.OrganizationID,
o.OrganizationName,
os.WhiteLabelPartnerID,
s.StateName
INTO #WLPO
from [Org].[Organizations] o
join [Org].[OrganizationStates] os on o.OrganizationID=os.OrganizationID --and os.WhiteLabelPartnerID = 1
join [Lookup].[States] s on os.StateID = s.StateID
join [Org].[PaymentOnFile] pof on pof.OrganizationID=o.OrganizationID
where os.WhiteLabelPartnerID in (2,3,4)
and os.StateID in (1, 2, 3)
and o.OrganizationID = 7613
select * from #WLPO
Select
o.OrganizationID,
o.OrganizationName,
os.WhiteLabelPartnerID,
s.StateName
INTO #org
from [Org].[Organizations] o
join [Org].[OrganizationStates] os on o.OrganizationID=os.OrganizationID --and os.WhiteLabelPartnerID = 1
join [Lookup].[States] s on os.StateID = s.StateID
join [Org].[PaymentOnFile] pof on pof.OrganizationID=o.OrganizationID
where 1=1--os.WhiteLabelPartnerID = 1
and os.StateID in (1, 2, 3)
and o.OrganizationID = 7613
select * from #org
Select
OrganizationID,
count(OrganizationID) AS CountOrgTypes
INTO #OrgCount
from #org
where OrganizationID = 7613
group by OrganizationID
select * from #OrgCount
Select distinct
ct.OrganizationID,
ok.OrganizationName,
ct.CountOrgTypes,
case when ct.CountOrgTypes = 2 then wlp.WhiteLabelPartnerID
when ct.CountOrgTypes = 1 then ok.WhiteLabelPartnerID
END AS CustomerTypeCode,
case when ct.CountOrgTypes = 2 then wlp.StateName
when ct.CountOrgTypes = 1 then ok.StateName END As OrgState
INTO #cc
from #org ok
left join #WLPO wlp on wlp.OrganizationID=ok.OrganizationID
join #OrgCount ct on wlp.OrganizationID=ct.OrganizationID
select * from #cc
Select
OrganizationID,
OrganizationName,
CountOrgTypes,
case when CustomerTypeCode = 1 then 'Direct'
when CustomerTypeCode = 2 then 'Partner1'
when CustomerTypeCode = 3 then 'Partner2'
when CustomerTypeCode = 4 then 'Partner3' ELSE Null END AS CustomerType,
OrgState
from #cc
order by OrganizationName asc
DirectCustomer
CustomerwithBoth

postgresql selecting entries that appear more than 4 times

I have 2 tables, one contains people and another contains enrolments with a reference to the people id's, I have to create a view where only results that appear in the enrolments more than 4 times are included. I'm okay with making the view and getting the columns I need but the syntax is so confusing and the course I'm doing is very unclear. I think I need to use count() but I can't get it to count what I need it to. How do you output a table that only includes the people that appear in the enrollment table more than 4 times? Sorry if the question is a bit bad I'm extremely sleep deprived and confused.
edit: here are some example tables and what I tried to do-
enrolment table relevant info:
id
student
462583
1010093
464457
1010093
469823
1010093
471345
1010093
473239
1010093
475371
1010093
477419
1010093
479797
1010093
572312
1010138
577147
1010138
578866
1010138
580596
1010138
582497
1010138
so students 1010093 and 1010138 would fit the criteria because they appear more than 4 times, but there are many entries that do not appear more than 4 times.
people table relevant info:
(id is the id that enrolment refers to in student column).
id
uniid
name
10000019
8758024
Emery Schubert
10000021
9808692
Ann Moore
10000025
9833783
Zhen-Tian Chang
10000026
7610575
John Carrick
10000035
9837669
Pamela Mort
10000037
9049091
Sami Korell
10000049
9869271
Mengistu Amberber
10000051
9375982
Colin Fong
10000053
9146607
Dianne Montgomerie
10000073
9804805
Grant Walter
1010093
2220747
Barbara Fremder
1010138
2240781
Say-Kit Ezergailis
1011114
2119574
Evangelos McDonald
1011293
2291530
Grace Hoekstra
1011474
2261154
Chee Jairaj
my attempt was this:
create or replace view Q1(uniid,name) as
select people.uniid, people.name
from people left outer join enrolments on (people.id = enrolments.student)
group by people.uniid, people.name having count(enrolments.student) > 4;
here's a sample of the output:
uniid
name
3100280
Mia Wiech
3225571
Cora Prochaska
3335780
Vinh Ha
3255146
Moyang Liu Hongtao
3365147
Frances Ellers
3327487
Keerati Meechowna
3397549
Shane Dinham
3372084
Benjamin Tenenbaum
3252837
Kayserline McFarlane
3350110
Jose Varas
3258061
Alison Lettich
3345581
Snehal Sethu
This was the expected output in full:
uniid
name
3001394
Jeffrey Caldwell
3087372
Philip Lun
3108948
Sugianto Arsie
3122927
Seaton Warburton
3132303
Amy Berg
3134147
Hannah Kola
3160479
Ksenia Mardakhaeva
3163349
Kerry Plant
3173796
Adam Rositano
3187169
Giles Erol
3207313
Livio Tjia
3209530
Su Song
3229297
Yoke Anthoney
3245227
Pollyanna Risk
3272803
Jesus Ferrer
3294743
Deviani Hongganata

How to fetch doctype eg: address or tax rule

I want to fetch the doctype. How do I do this? I want to add a separate column which will give doctype such as sales order, purchase order etc. The first line gives me error what query should be fired. Please help I am new to ERP Next.
SELECT
AD.ref_doctype AS “Doctype:Link/User:120”,
AD.name AS “Doc#:Link/Doctype:120”,
AD.owner AS “Created By:Link/User:120”,
AD.modified AS “Modified On:Date:120”
FROM tabAddress AS AD
WHERE
DATEDIFF(now(),AD.modified) BETWEEN 1 AND 30
UNION ALL
SELECT
TR.name AS “Doc#:Link/Doctype:120”,
TR.owner AS “Created By:Link/User:120”,
TR.modified AS “Modified On:Date:120”
FROM tabTax Rule AS TR
WHERE
DATEDIFF(now(),TR.modified) BETWEEN 1 AND 30
UNION ALL
SELECT
IT.name AS “Doc#:Link/Doctype:120”,
IT.owner AS “Created By:Link/User:120”,
IT.modified AS “Modified On:Date:120”
FROM tabItem AS IT
WHERE
DATEDIFF(now(),IT.modified) BETWEEN 1 AND 30
It isn't completely clear to me what you mean by docType field.
Are you wanting a result like this?
Doctype:Link/User:120|Doc#:Link/Doctype:120|Created By:Link/User:120|Modified On:Date:120|
---------------------|---------------------|------------------------|--------------------|
Email Account |Jobs |Administrator | 2019-12-04 06:07:55|
Email Account |Notifications |Administrator | 2019-12-01 05:25:53|
Email Account |Replies |Administrator | 2019-12-01 05:25:53|
Email Account |Sales |Administrator | 2019-12-04 06:07:55|
Email Account |Support |Administrator | 2019-12-04 06:07:55|
Here's the select :
set #docType = "Email Account";
SELECT
#tabDocType AS `Doctype:Link/User:120`,
AD.name AS `Doc#:Link/Doctype:120`,
AD.owner AS `Created By:Link/User:120`,
AD.modified AS `Modified On:Date:120`
FROM `tabEmail Account` AS AD
Note the backticks on the field aliases! All these have different meanings in SQL:
"
'
`
The last one, backtick, is used to refer to database entities. You were trying to use “Doctype:Link/User:120” with double quotes, which declare plain text. Using backtick converts the alias into a db entity which can be referred to from elsewhere.
MariaDb doesn't allow the use of variables as table names directly, but you can do it using prepared statements, like this:
set #docType = "Email Account";
set #tabDocType = CONCAT('tab', #docType);
SET #sql_text = concat('
SELECT
"', #docType, '" AS `Doctype:Link/User:120`
, AD.name AS `Doc#:Link/Doctype:120`
, AD.owner AS `Created By:Link/User:120`
, AD.modified AS `Modified On:Date:120`
FROM `', #tabDocType, '` as AD;
');
PREPARE stmt FROM #sql_text;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
The table name is now also specified by a variable, created from concatenation of 'tab' with the docType declared before.
You get the same result as above but -- you avoid accidentally changing the table name in one place but not in the other when editing some time in the future.
to fetch doctype name you have to give the linked doctype name, For example,
select
IT.name as "IT No:Link/IT:120"

Question on SQLite database query on INNER join

I'm new for SQLite DB, I have three tables, M_Clinic, M_Healees and T_Treatments.. In following query, I have used two inner joins from T_Treatments (Transaction) to two of the master refernce tables M_Clinic and M_Healees.. There is a record on both T_Treatments and M_Healees with Healee_ID column with data value 1. However for some reason it is not pulling the record out.. If I do the same query without "where T_Treatments.Healee_ID = 1", the query pulls out the record.. Am I missing anything?
select M_Healees.Healee_ID,
M_Healees.Name,
M_Healees.PhoneNumber,
M_Healees.HealthInformation,
M_Healees.Address,
M_Healees.Clinic_ID,
M_Clinic.Name,
M_Clinic.Address,
T_Treatments.TreatmentDate,
T_Treatments.Symptoms,
T_Treatments.ACUPoint
from T_Treatments
INNER JOIN M_Clinic ON M_Healees. Clinic_ID = M_Clinic. Clinic_ID
INNER JOIN M_Healees ON M_Healees.Healee_ID = T_Treatments.Healee_ID
where T_Treatments.Healee_ID = 1
These are the data on my tables:
TABLE : M_clinic
<table><tr><td>Clinic_ID|</td><td>Name|</td><td>Address|</td></tr>
<tr><td>PK|</td><td>Salem Clinic|</td><td>No.52, Seerangan Street-2, Dhadhagapatti gate, Salem|</td></tr>
</table>
TABLE : M_Healees
<table><tr><td>Healee_ID|</td><td>Name|</td><td>PhoneNumber|</td><td>HealthInformation|</td><td>Address|</td><td>Clinic_ID|</td></tr>
<tr><td>1|</td><td>Narmada|</td><td>9988|</td><td>Body Pains|</td><td>Nethimedu|</td><td>PK|</td></tr>
</table>
TABLE : M_Treatments
<table><tr><td>Healee_ID|</td><td>TreatmentDate|</td><td>Symptoms|</td><td>ACUPoint|</td></tr>
<tr><td>1|</td><td>10/08/2019|</td><td>Body Pain|</td><td>LU5|</td></tr>
</table>

Using inner join with msqli

I have tried every single expalnation on this but I keep getting errors please help me out guys this might have been answered but its over a week I keep struggleing with it.
I have two Tables
Student and Certificate
the two tables have a relationship on student, i have a unique column student reg no and on certificate student reg which are bot the same values.
I am trying to select data from the two table into a table in html but its not working
here is my statement
<?php
$sql = "SELECT * s.studentsregno
s.fullname
s.program
c.certificateno AS cert_no
c.dateofissue AS date_ofissue
c.status AS pick_status
FROM student s
JOIN certificate c on c.studentreg = s.studentregno";
$result = $db->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()){
echo
'<tr class="odd gradeX">
<td>'.$row["studentregno"].'</td>
<td>'.$row["fullname"].'</td>
<td>'.$row["program"].'</td>
<td class="center">'.$row["cert_no"].'</td>
<td class="center">'.$row["date_ofissue"].'</td>
<td class="center">Picked</td>
</tr>';
}
}
$db->close();
?>
Maybe try by adding some coma and not using * the other, something like this :
SELECT s.studentsregno, s.fullname, s.program, c.certificateno AS cert_no, c.dateofissue AS date_ofissue, c.status AS pick_status FROM student s JOIN certificate c on (c.studentreg = s.studentregno);
I'm not a big fan of renaming like this the table too so if it still doesn't work, try by remplacing s and c by the real table name and just use this surname when you really need it (a join beetween two from the same table)