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)
Related
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
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>
my table is:
id | id_fruit | name
2|1|orange|
3|1|apple|
23|1|banana|
34|1|ananas|
35|3|kiwi|
I want to extract all name where id_fruit=1 and print them in a form field as values in this way:
orange, apple, banana, ananas
I've tried
select concat_ws(', ', name) from fruits where id_fruit=1 but doesn't work.
Ho can I do it? thanks!
User Group_Contact function in mysql
Select group_concat(name) from my table where fruit_id=1
I think you misunderstood the concat_ws() use.
MySQL documentation about this function explains you must give every argument to join at once.
So you sql statement has no effect.
As Raja says, you can go with your back-end programming language (such as php, python or whatever you use).
You just select without the concat_ws and do the join on the back-end side.
It seems concat_ws() main use is to concat two fields from the same entry in mysql, such as the firstname and lastname (fields) for example of the same user (entry).
Simply through PHP, you can do like that!
You can while loop all the names like this.
PHP
$in = 0;
$fruits = "";
while($row = mysqli_fetch_assoc($exeutedquery)){
if($i == 0){
$fruits += $row['name'];
$i++;
}else{
$fruits += ",".$row['name'];
}
}
AND FOR SQL
SELECT STUFF
(
(select ',' + name
from
fruits
where
ID in (1,2,3)
FOR XML PATH('')
),1,1,''
)
BAM!
I am attempting to get all the records from a table where trans_type='RM' but if there is no trans_type='RM' i want to return all the records where trans_type = 'AD'
Technically im using xtupls MetaSQL on a PostgreSQL server so a solution using either is great I can upload my metaSQL statement need be but I really just need a way to do
-- Group: lotserial
-- Name: detail
-- Notes:
-- Copyright (c) 1999-2014 by OpenMFG LLC, d/b/a xTuple.
-- See www.xtuple.com/EULA for the full text of the software license.
SELECT ls_number,
ls_notes,
formatlotserialnumberbarcode(ls_number) AS lotserial_barcode,
item_number,
item_descrip1,
item_descrip2,
charass_char_id,
charass_value,
poitem_id,
poitem_vend_item_descrip,
char_name,
formatqty(itemloc_qty) as lotqty,
lshist.*
FROM
itemloc,
ls
JOIN item ON (item_id=ls_item_id)
LEFT JOIN charass ON (charass_target_id=ls_id)
LEFT JOIN "char" ON (char_id=charass_char_id),
lshist (<? value("itemid") ?>,<? value("warehouseid") ?>,ls_number,
<? value("pattern") ?>,<? value("transType") ?>,<? value("startDate") ?>,
<? value("endDate") ?>,<? value("trace") ?>,1)
LEFT JOIN pohead ON(pohead_number=(TRIM(SUBSTRING(lshist_ordernumber FROM '-.*-'),'-')))
LEFT JOIN poitem ON(poitem_pohead_id=pohead_id)
<? if exists('ls_id') ?>
WHERE ls_id=<? value("ls_id") ?>
<? endif ?>
<? if exists('ls_number') ?>
WHERE ls_number=<? value("ls_number") ?>
<? endif ?>
AND lshist_warehous_code='PS'
<? if exists(TRIM(SUBSTRING(lshist_ordernumber FROM '.*-'),'-')='PO')?>
AND poitem_linenumber = CAST(TRIM(SUBSTRING(lshist_ordernumber FROM '[^-]*$'),'-') AS INTEGER)
<? endif ?>
AND ls_id = itemloc_ls_id
AND charass_target_type = 'LS'
/*
<? if exists(lshist_transtype='RM')?>
AND lshist_transtype='RM'
<? elseif exists(lshist_transtype='AD')?>
AND lshist_transtype='AD'
<? elseif exists(lshist_transtype='RL')?>
AND lshist_transtype='RL'
<? elseif exists(lshist_transtype='SH')?>
AND lshist_transtype='SH'
<? elseif exists(lshist_transtype='IM')?>
AND lshist_transtype='IM'
<? elseif exists(lshist_transtype='TR')?>
AND lshist_transtype='TR'
<? elseif exists(lshist_transtype='RP')?>
AND lshist_transtype='RP'
<? endif ?>
You could use a common table expression:
WITH rm AS (
SELECT * FROM my_table WHERE trans_type = 'RM'
)
SELECT *
FROM data
UNION ALL
SELECT * FROM my_table
WHERE trans_type = 'AD'
AND NOT EXISTS (
SELECT * FROM rm
)
This will avoid the second scan as can be seen in an EXPLAIN ANALYZE call, but there's still a little overhead compared to making the decision in the client, probably due to the CTE materialization (which is PostgreSQL-specific).
I've benchmarked this for an small data set. There seems to be a 5% - 10% overhead in PostgreSQL over running two queries from pgplsql. So, in most cases, and for simple queries like this one, Laurenz's solution is preferrable.
There may be more complex query setups, where the single query is preferrable to two separate queries, as the single query can re-use intermediate results.
I would just run two queries, the first with WHERE trans_type='RM', and only run a second query with WHERE trans_type='AD' if the first one returns an empty result.
I think that trying to squish that into a single query would make things overly complicated and probably also would not be faster – I cannot think of a way that avoids a second scan.
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 =  ". $row['staff_id']."   from department  ".$row['dept_name']."       is    ". $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