Question on SQLite database query on INNER join - android-sqlite

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>

Related

Is there a way to modify this query and make it more simple?

I am working on a MySQl data base and have several tables that I need to join based on a temporary table that I created (layer_2).
Each table that I join has the join key for the next table;
E.g. layer_2 joins with "colegios" table, then the result will have the join key for the next table "ciudades". and finally the result will have the Join key for the next table "departamentos".
from what I got it works but I want to know if there is a way to simplify this?
Select
cuestionario_id,
tiempo,
nro_preguntas,
nro_preguntas_correctas,
tipo_usuario,
fecha_creacion_cuestionario,
con_tiempo,
det_cuestionarios_id,
respuesta_seleccionada,
pregunta_id,
enunciado,
respuesta_correcta,
usuarios_id,
dominio_correo,
tipo_institucion,
materia,
tematica,
area,
nombre_colegios,
direccion_colegios,
nombre_ciudades,
redsaber.departamentos.nombre as nombre_departamentos
From
(
Select
cuestionario_id,
tiempo,
nro_preguntas,
nro_preguntas_correctas,
cuestionable_id,
tipo_usuario,
fecha_creacion_cuestionario,
con_tiempo,
det_cuestionarios_id,
respuesta_seleccionada,
pregunta_id,
enunciado,
respuesta_correcta,
usuarios_id,
dominio_correo,
cole_ciud_id,
tipo_institucion,
materia,
tematica,
area,
combine_ciudad_id,
nombre_colegios,
direccion_colegios,
redsaber.ciudades.nombre as nombre_ciudades,
departamento_id
From
(Select
cuestionario_id,
tiempo,
nro_preguntas,
nro_preguntas_correctas,
cuestionable_id,
tipo_usuario,
fecha_creacion_cuestionario,
con_tiempo,
det_cuestionarios_id,
respuesta_seleccionada,
pregunta_id,
enunciado,
respuesta_correcta,
usuarios_id,
dominio_correo,
cole_ciud_id,
tipo_institucion,
materia,
tematica,
area,
ifnull(redsaber.colegios.ciudad_id,cole_ciud_id) as combine_ciudad_id,
redsaber.colegios.nombre as nombre_colegios,
redsaber.colegios.direccion as direccion_colegios
From layer_2
Left Join redsaber.colegios on layer_2.cole_ciud_id = redsaber.colegios.id
AND tipo_institucion = 'Colegio') as layer_3
Left Join redsaber.ciudades on combine_ciudad_id = redsaber.ciudades.id) as layer_4
Left Join redsaber.departamentos on departamento_id = redsaber.departamentos.id

SQL query for finding Foreign key constraints

I have one column , and i want to find in How many table that column used as foreign and also name of the table in which that column is used. I have PostgreSQL database . and i am using PG admin tool
select R.TABLE_NAME from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE u
inner join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS FK
on U.CONSTRAINT_CATALOG = FK.UNIQUE_CONSTRAINT_CATALOG
and U.CONSTRAINT_SCHEMA = FK.UNIQUE_CONSTRAINT_SCHEMA
and U.CONSTRAINT_NAME = FK.UNIQUE_CONSTRAINT_NAME inner join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE R
ON R.CONSTRAINT_CATALOG = FK.CONSTRAINT_CATALOG
AND R.CONSTRAINT_SCHEMA = FK.CONSTRAINT_SCHEMA
AND R.CONSTRAINT_NAME = FK.CONSTRAINT_NAME WHERE U.COLUMN_NAME='M_InLine_ID'
AND U.TABLE_NAME = 'M_InLine'
I tried above query but it snot given any output
Please help me out

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)

Inner join 4 columns with different server .., different database ., and different table

I have 3 server:
ECPAYSERV2
POSSQLSERVER
ECPAYSERV1
3 databases
ECPNWEB
ECPNPOS
ECPNDB
and 3 tables
account
branch
terminal
I have also finish to link the server :
ECPAYSERV2.ECPNWEB.dbo.account
POSSQLSERVER.ECPNPOS.dbo.branch
ECPAYSERV1.ECPNDB.dbo.terminal
The fields that should be seen would be the following
AccountID = ECPAYSERV2.ECPNWEB.dbo.account.(accntid)
BranchID = ECPAYSERV2.ECPNWEB.dbo.branch.(branchid)
TID = ECPAYSERV1.ECPNDB.dbo.terminal.(TID)
Store # = POSSQLSERVER.ECPNPOS.dbo.branch.(Storeno)
Store name = ECPAYSERV2.ECPNWEB.dbo.account.(accountholder)
Branch name = ECPAYSERV2.ECPNWEB.dbo.brachn.(branchname) , POSSQLSERVER.ECPNPOS.dbo.branch.(branchname) , ECPAYSERV1.ECPNDB.dbo.terminal.(retail_store_code)
The data enclosed with parethesis are the columns.,
To be more clear of all this are the columns in the table..
Table : Account
|Accntid|managedby|Accountholder|Description|AccountType|ContactPerson|ContactNumber|EmailAddress|
Table : Branch
|BranchID|BranchName|AccountID|StoreNo|Description|Status|
Table : Terminal
|TerminalID|TID|retail_store_code|t_distributor_code|
All I want is to Get this output with RIGHT data ..
|accntid|accountholder|tid|storeno|branchname|branchid|
This is what I tried for but not getting the RIGHT output
SELECT account.accntid,account.accountholder,terminal.tid,branch.storeno,branch.branchname,branch1.branchid
FROM ECPAYSERV2.ECPNWEB.dbo.account as account
INNER JOIN POSSQLSERVER.ECPNPOS.dbo.branch as branch
ON account.accntid=branch.branchid
INNER JOIN ECPAYSERV2.ECPNWEB.dbo.branch as branch1
ON account.accntid=branch1.branchid
INNER JOIN ECPAYSERV1.ECPNDB.dbo.terminal as terminal
ON account.accntid=terminal.tid
Please help me about This matter Thanks :(
SELECT
account.accntid, account.accountholder, terminal.tid, branch.storeno, branch.branchname, branch1.branchid
FROM
ECPAYSERV2.ECPNWEB.dbo.account as account
INNER JOIN POSSQLSERVER.ECPNPOS.dbo.branch as branch
ON account.accntid=branch.AccountID
INNER JOIN ECPAYSERV2.ECPNWEB.dbo.branch as branch1
ON account.accntid=branch1.AccountID
INNER JOIN ECPAYSERV1.ECPNDB.dbo.terminal as terminal
ON account.accntid=terminal.???
the ??? is not correct, but it is not clear which column of terminal is a FK for account.

How to perform Linq to Entites Left Outer Join

I have read plenty of blog posts and have yet to find a clear and simple example of how to perform a LEFT OUTER JOIN between two tables. The Wikipedia article on joins Join (SQL) provides this simple model:
CREATE TABLE `employee` (
`LastName` varchar(25),
`DepartmentID` int(4),
UNIQUE KEY `LastName` (`LastName`)
);
CREATE TABLE `department` (
`DepartmentID` int(4),
`DepartmentName` varchar(25),
UNIQUE KEY `DepartmentID` (`DepartmentID`)
);
Assume we had a EmployeeSet as an employee container ObjectSet<Employee> EmployeeSet and a DepartmentSet ObjectSet<Department> DepartmentSet. How would you perform the following query using Linq?
SELECT LastName, DepartmentName
FROM employee e
LEFT JOIN department d
ON e.DepartmentID = d.DepartmentID
I would write this, which is far simpler than join and does exactly the same thing:
var q = from e in db.EmployeeSet
select new
{
LastName = e.LastName,
DepartmentName = e.Department.DepartmentName
};
You need to use the DefaultIfEmpty method :
var query =
from e in db.EmployeeSet
join d in db.DepartmentSet on e.DepartmentID equals d.DepartmentID into temp
from d in temp.DefaultIfEmpty()
select new { Employee = e, Department = d };