I have a three tables to join. (users, user_module, module). from users table i want to get Fname field. from user_module table i want to get assign module number for the student. from module table i want to get module field.(name of the module).
users table
id Fname
1 Asela
user_module table
studentID moduleID
1 1
module table
id module
1 AST
my code to get result from the tables
$assignModule = new SqlDataProvider([
'sql'=> "SELECT Fname AS Student_name, A3.moduleID As Module_no, A2.module As Module_name FROM users A1"
."LEFT JOIN user_module A3 ON A3.studentID = A1.id"
."LEFT JOIN module A2 ON A3.moduleID = A2.id "
. "WHERE A2.id IN (SELECT studentID FROM user_module)",
error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'A1.idLEFT' in 'on clause'
try this query
$sql = "SELECT A1.Fname As Student_name,
A3.moduleID As Module_no,
A2.module As Module_name,
FROM
users A1
LEFT OUTER JOIN user_module A3 ON A1.id=A3.studentID
LEFT OUTER JOIN module A2 ON A3.moduleID=A2.id
WHERE id IN (SELECT studentID FROM user_module)";
I would recommend doing as Bloodhound said and change how you're doing your quotes. The way you are concatenating your query string is causing the text to bump together, likely breaking the query. For example in your query:
"SELECT Fname AS Student_name, A3.moduleID As Module_no, A2.module As Module_name FROM users A1"
."LEFT JOIN user_module A3 ON A3.studentID = A1.id"
."LEFT JOIN module A2 ON A3.moduleID = A2.id "
. "WHERE A2.id IN (SELECT studentID FROM user_module)",
It's being read as:
SELECT Fname AS Student_name, A3.moduleID As Module_no, A2.module As Module_name FROM users A1LEFT JOIN user_module A3 ON A3.studentID = A1.idLEFT JOIN module A2 ON A3.moduleID = A2.id WHERE A2.id IN (SELECT studentID FROM user_module)
Related
Have 2 tables
User table with
user chat_id, kunnr (cust_code) & vkorg
Delivery table with
PO # and other delivery info, cust_code & sales_org
I want to verify if the user cust_code and sales_org match with shipmentdel's table
One user can belong to multiple sales org and a PO can have many delivery orders
Below is my working SQL statement, can this be simplified ?
SELECT * FROM ( SELECT a.* FROM ztelegram.ShipmentDel a
WHERE a.Cust_PO = 'WPO-01502') as t1
LEFT JOIN
( SELECT b.* FROM ztelegram.noti_setting b WHERE b.chat_id = '12345667' ) as t2
ON t1.sales_org = t2.vkorg AND t1.Sold_to_party_name = t2.kunnr
Without the need of t1 & t2, isnt the query working? Please check.
SELECT
*
FROM
ztelegram.ShipmentDel a
LEFT JOIN ztelegram.noti_setting b ON a.sales_org = b.vkorg AND a.Sold_to_party_name = b.kunnr
WHERE
a.Cust_PO = 'WPO-01502'
and b.chat_id = '12345667'
I have a list of tables as follows
tbluser
(userid, username)
tblmember
(memberid, fk_userid)
tblleader
(leaderid, fk_userid)
tbltask
(taskid, fk_memberid, fk_leaderid, taskname, taskstatus)
here the user will be a member and can be a leader too
i want to generate a query to show the list of the assigned task to the members and their leaders name.
Sl. No | Member Name | Task Name | Task Status | Leader Name
-------------------------------------------------------------------------
I am having joining issues as both member and leader table is referenced to the same table(user table).
Can anybody help me to get the query to show the data.
You question wasn't clear enough ,but i think your looking for this.
SELECT taskname,taskstatus,username
FROM tbltask,tblmember,tblleader,tbuser
WHERE (tblmember.memberid = tbltask.fk_memberid) AND (tbltask.fk_leaderid =
tblleader.leaderid) AND (tblleader.fk_userid = tbluser.userid);
SELECT
*
FROM
table1
INNER JOIN
tabel2 ON table2.id = table1.id
INNER JOIN
table3 ON table3.id = table2.id;
Sample data might have helped you better here, But I think you can try the below query -
SELECT userid AS "Sl. No",
username "Member Name",
taskname "Task Name",
taskstatus "Task Status",
Leader Name
FROM tbluser TU
JOIN tblmember TM ON TM.fk_userid = TU.userid
JOIN tblleader TL ON TL.fk_userid = TU.userid
JOIN tbltask TT ON TL.leaderid TT.fk_leaderid
AND TM.memberid = TT.fk_memberid;
I am trying to run a select statement on two different servers that have some matching primary keys. I want to be able to select the primary key but add the prefix 'A' to every record
ex:
ID "to this" ID
1 A1
2 A2
3 A3
This is what I have for the query
select 'A' + CAST (a.id AS VARCHAR(25)) as ID,a.*,':', b.*,':',c.*,':', d.*,':', e.*,'REPORTDT', g.*
from labgen.order_ a
join patient b on a.id = b.chart
join insurance c on b.ins1 = c.code
join client d on d.num = a.client1
join salesgrp e on d.salesgroup = e.num
join reportdt g on a.accession = g.accession
Try this in the select statement:
select CONCAT('A', a.id) as ID, a.*,':', //etc
This link will also help.
i have the following tables:
Technician
Tech_ID,First_Name,Last_Name
RT_QUEUE_Delta
Tech_ID, RT_Complete` (references a `Tech_ID` in `Technician`).
I need to get the data from a row in RT_Queue_Delta where RT_Completed = ?? but in my output I need to have the First_Name and Last_name that correlates with Tech_id and RT_Completed.
I can match one but I don't know how to match both. I tried:
select RTTech.First_Name as RT_First_Name,
RTTech.Last_Name as RT_Last_Name
from Technician as RTTech
Join RT_Queue_Delta as RT
on RT.RT_Completed = RTTech.Tech_ID
You can join to the Technician table multiple times:
select d.tech_id, t.first_name, t.last_name,
d.rt_completed as completed_id,
t2.first_name as completed_first_name,
t2.last_name as completed_last_name
from RT_QUEUE_Delta d
join Technician t on d.tech_id = t.tech_id
join Technician t2 on d.RT_Completed = t2.tech_id
I have the following query which returns data only if the join exists. How do I return from the last joined table (#__unis) datas, even if there is no relationship between those tables without to write another query?
select * from #__unis_faculties AS faculty
join #__unis_subjects AS subject ON subject.faculty = faculty.id
join #__unis AS uni ON uni.id= subject.university
where uni.id = 1
table structure http://sqlfiddle.com/#!2/19add
use LEFT JOIN instead of join
select * from #__unis_faculties AS faculty
join #__unis_subjects AS subject ON subject.faculty = faculty.id
right join #__unis AS uni ON uni.id= subject.university
where uni.id = 1
Try this..
select * from #__unis_faculties AS faculty
join #__unis_subjects AS subject ON subject.faculty = faculty.id
left join #__unis AS uni ON ( uni.id= subject.university AND uni.id = 1 )