sqlite multiple table joining...how? - sqlite

I have three tables doodhiya, doodhdata and cashdata.
I am trying to join these and fetch some needed data by this code but not succeed
SELECT dname,ddate,dmonth,dyear,dmilk,uid
FROM doodhiya
INNER JOIN doodhiya.dhid = doodhdata.ddhid
INNER JOIN doodhdata.dhid = cashdata.uid
WHERE (dname='$mik' AND dmonth='$mikdatem' AND dyear='$mikdatey')
ORDER BY ddate ASC
What I have to do?

You are missing the table name and the ON keyword in your join
SELECT dname,ddate,dmonth,dyear,dmilk,uid
FROM doodhiya
INNER JOIN doodhdata ON doodhiya.dhid = doodhdata.ddhid
INNER JOIN cashdata ON doodhdata.dhid = cashdata.uid
WHERE (dname='$mik' AND dmonth='$mikdatem' AND dyear='$mikdatey')
ORDER BY ddate ASC
A note on this part of the question:
Pls suggest me what I have to do
The syntax for a join is documented in the manual (and millions of SQL references in the web). So the best thing you can do the next time is to first consult the manual and/or a SQL reference.

SELECT retailername,
productname,
qty,
stock,
price,
discount
FROM temptablename,
productmaster,
retailermaster
WHERE temptablename.pid = productmaster.productid
AND temptablename.rid = retailermaster. retailercode

SELECT dname,
ddate,
dmonth,
dyear,
dmilk,
uid
FROM doodhdata,
cashdata
WHERE doodhiya.dhid = doodhdata.ddhid
AND doodhdata.dhid = cashdata.uid
AND dname='$mik'
AND dmonth='$mikdatem'
AND dyear='$mikdatey'
ORDER BY ddate ASC

Related

Getting a min(date) AND max(date) AND their respective titles

I have three tables that I would like to select from
Table 1 has a bunch of static information about a user like their idnumber, name, registration date
Table 2 has the idnumber of the user, course number, and the date they registered for the course
Table 3 has the course number, and the title of the course
I am trying to use one query that will select the columns mentioned in table 1, with the most recent course they registered (name and date registered) as well as their first course registered (name and date registered)
Here is what I came up with
SELECT u.idst, u.userid, u.firstname, u.lastname, u.email, u.register_date,
MIN(l.date_inscr) as mindate, MAX(l.date_inscr) as maxdate, lc.coursename
FROM table1 u,table3 lc
LEFT JOIN table2 l
ON l.idCourse = lc.idCourse
WHERE u.idst = 12787
AND u.idst = l.idUser
And this gives me everything i need, and the dates are correct but I have no idea how to display BOTH of the names of courses. The most recent and the first.
And help would be great.
Thanks!!!
You can get your desired results by generating the min/max date_inscr for each user in a derived table and then joining that twice to table2 and table3, once to get each course name:
SELECT u.idst, u.userid, u.firstname, u.lastname, u.email, u.register_date,
l.mindate, lc1.coursename as first_course,
l.maxdate, lc2.coursename as latest_course
FROM table1 u
LEFT JOIN (SELECT idUser, MIN(date_inscr) AS mindate, MAX(date_inscr) AS maxdate
FROM table2
WHERE idUser = 12787
) l ON l.idUser = u.idst
LEFT JOIN table2 l1 ON l1.idUser = l.idUser AND l1.date_inscr = l.mindate
LEFT JOIN table3 lc1 ON lc1.idCourse = l1.idCourse
LEFT JOIN table2 l2 ON l2.idUser = l.idUser AND l2.date_inscr = l.maxdate
LEFT JOIN table3 lc2 ON lc2.idCourse = l2.idCourse
As #BillKarwin pointed out, this is more easily done using two separate queries.

How can i do pl/sql Update with join

how can i update a table column from another table. here is my code :
update table1 hn
set hn.changeColumn=es.changeColumn
from table1 hn
inner join table2 es on es.x=hn.xand es.rol_id=hn.rol_id
where hn.x= es.x and hn.rol_id = es.rol_id
i wanna set table1's column(changeColumn) values with table2's column(changeColumn) values
how can i do this. thanks
If you want to convert this query to Oracle, You need a MERGE INTO statement -
MREGE INTO table1 hn
USING table2 es
ON (es.x=hn.x and es.rol_id=hn.rol_id)
WHEN MATCHED THEN
UPDATE
SET hn.changeColumn=es.changeColumn
One valid way to write this query on Oracle would be:
UPDATE table1 t1
SET changeColumn = (SELECT t2.changeColumn
FROM table2 t2
WHERE t1.x = t2.x AND t1.rol_id = t2.rol_id);
This assumes that your join condition would only generate at most a single pair of mstching records from the self join. If not, then we would have to modify the logic.

SQLITE ordering group_concat with a left join

I have the following query
let Query = "SELECT LEVELS1.ID, LEVELS1.NAME, GROUP_CONCAT(FRAMEWORKLEVELS1.NAME,'&/?') AS DATA FROM LEVELS1 LEFT JOIN FRAMEWORKLEVELS1 ON LEVELS1.ID = FRAMEWORKLEVELS1.LEVELID WHERE FRAMEWORKID = :ID GROUP BY FRAMEWORKID"
which produces the following
DATA = "Level1&/?Level4&/?Level2&/?Level3";
ID = 1;
NAME = "Title";
What i want to do though is order the GROUP_CONCAT but how do i do this in SQLITE?
I have found the following link
Sqlite group_concat ordering
but i can't work out how to get the left join in there
Thanks
Just get all the data you want, sort it, and then aggregate it:
SELECT ID,
Name,
group_concat(LevelName, '&/?') AS Data
FROM (SELECT Levels1.ID AS ID,
Levels1.Name AS Name,
FrameworkLevels1.Name AS LevelName
FROM Levels1
LEFT JOIN FrameworkLevels1 ON Levels1.ID = FrameworkLevels1.LevelID
WHERE FrameworkID = :ID
ORDER BY ...);

Update field using inner join

I am trying to update a field in a table from another table using INNER JOIN. Here is the code:
UPDATE TestResults
INNER JOIN Distractors
ON TestResults.DistractorID = Distractors.ID
SET TestResults.DistractorValue = Distractors.DistractorValue
This does not work I don't know why! Any idea? When I run the query I get the following error
There was an error parsing the query. [ Token line number = 2,Token line offset = 1,Token in error = INNER ]
Not all databases support join syntax with update. And when they do, the syntax differs. Here is a way to do your query without an explicit join using standard SQL:
UPDATE TestResults
set DistractorValue = (select max(d.DistractorValue)
from Distractors d
where TestResults.DistractorValue = d.DistractorValue
)
where exists (select 1
from Distractors d
where TestResults.DistractorValue = d.DistractorValue
);
The max() is only needed if there could be more than one matching row.
The where is only needed if the join is intended to do filtering as well as matching.
UPDATE TestResults
SET TestResults.DistractorValue = Distractors.DistractorValue
FROM TestResults
INNER JOIN Distractors
ON TestResults.DistractorID = Distractors.ID
You use the Updated table in the Inner join clause
ex:
UPDATE TestResults
SET TestResults.DistractorValue = Distractors.DistractorValue
FROM TestResults INNER JOIN Distractors
ON TestResults.DistractorID = Distractors.ID

Asp.Net multiple join tables issue from Sqldatasoruce Query Builder into one gridview

In Visual Studio 2010 with ASP.NET 4, I am trying to join several tables together to put the results in a gridview and details view with sqldatasource. In the sqldatasource wizard I have chosen to specify a custom SQL statement or stored procedure and then in the Query Builder to define complex queries such as JOINs, I have tried to generate a SQL statement to join the problem table with speficific columns from other tables. But when I try to test the query I get an error message which says "Cannot call methods on varchar". I am new to sql statements so please can you advise on what is wrong with the statement.
Here is the generated sql statement below
SELECT Problem.ProblemID, Problem.CustomerID, Problem.Summary,
Problem.DateLogged, Problem.DateUpdated, Status.Status, Priority.Priority,
Technician.Name, Technician.Surname, [Skill Group].[Skill Group],
HelpdeskOperator.Name AS Expr1,
HelpdeskOperator.Surname AS Expr2, Problem.NoteID, Problem.ResolutionID
FROM Problem
INNER JOIN Status ON Problem.StatusID = Status.Status.StatusID
INNER JOIN HelpdeskOperator ON
Problem.HelpdeskID = HelpdeskOperator.HelpdeskID AND Status.StatusID = HelpdeskOperator.StatusID
INNER JOIN Priority ON Problem.PriorityID = Priority.PriorityID
INNER JOIN [Skill Group] ON Problem.SkillGroupID = [Skill Group].SkillGroupID
INNER JOIN Technician ON Problem.ProblemID = Technician.ProblemID
AND Status.StatusID = Technician.StatusID AND
Priority.PriorityID = Technician.PriorityID
AND [Skill Group].SkillGroupID = Technician.SkillGroupID
Thank you in advance
Fixed your query:
SELECT p.ProblemID, p.CustomerID, p.Summary, p.DateLogged, p.DateUpdated, s.Status, pr.Priority, t.Name, t.Surname,
sg.* , ho.Name AS Expr1, ho.Surname AS Expr2, p.NoteID, p.ResolutionID
FROM Problem p
INNER JOIN Status s ON p.StatusID = s.StatusID
INNER JOIN HelpdeskOperator ho ON p.HelpdeskID = ho.HelpdeskID AND s.StatusID = ho.StatusID
INNER JOIN Priority pr ON p.PriorityID = pr.PriorityID
INNER JOIN [Skill Group] sg ON p.SkillGroupID = sg.SkillGroupID
INNER JOIN Technician t ON p.ProblemID = t.ProblemID AND s.StatusID = t.StatusID AND pr.PriorityID = t.PriorityID
AND sg.SkillGroupID = t.SkillGroupID
You had duplicate table identifier in your join clause Status.Status.StatusID
I doubt that your Skill Group table contains column [Skill Group] so changed it to return all values from Skill Group
I just think those were the errors, if not I will need more info about your query and table structure.
EDIT:
First it did not return anything for HelpdeskOperator, look at our query:
INNER JOIN HelpdeskOperator ho ON p.HelpdeskID = ho.HelpdeskID AND s.StatusID = ho.StatusID
that meanse that here is no such HelpdeskOperator record that is assigned to our problem AND statusid, so either problem id points to noexisting helpdeskoperator or statusid of
this operator is different that problem status id.
next is Skill Group
INNER JOIN [Skill Group] sg ON p.SkillGroupID = sg.SkillGroupID
again our problem point to no existing skill group
then Technican
INNER JOIN Technician t ON p.ProblemID = t.ProblemID AND s.StatusID = t.StatusID AND pr.PriorityID = t.PriorityID
AND sg.SkillGroupID = t.SkillGroupID
here is more work as more checks, technicas must be assigned to our problem with given status and priorityt and be in skill group, BUT our skill group is null? so to check if
there is technican for our problem remove AND sg.SkillGroupID = t.SkillGroupID so you get
INNER JOIN Technician t ON p.ProblemID = t.ProblemID AND s.StatusID = t.StatusID AND pr.PriorityID = t.PriorityID
and see if now we get any technican.
I hope this points you into right direction. You must be sure that there are matching record in every joining table.

Resources