Hi I am new to sql I just want to ask for advice my query is working fine it gives me my desired output but I wanted to make it in one query only I want to combine my delete query to the select query. I would really appreciate anyone can teach me how to do it
Delete from tblexample where RStatus = 'Done';
SELECT t3.RStatus,t1.Name,t2.gender,t1.Rnum,t1.NB,t1.fN
FROM table1 t1
INNER JOIN table2 t2
ON (t1.fN = t2.fln)
LEFT JOIN (
select * from tblexample t3
) AS t3 ON t1.NB = t3.NB where t3.RStatus is null
Like if possible I want to delete firs before I left join the tblexample
Related
I know there are similar questions to this, but the answers are not quite what I have been looking for. I have two MySQL tables, I'll say table1 and table2. The fields in table1 are StudentID , Grade and Total_Score. And the fields in table2 are StudentID ,Grade and Total_Score as well. What I want to do is SUM(table1.Total_Score) and add it to SUM(table2.Total_Score). So that Total_Score in table2 is accumulated with scores from table1.Total_Score.
I have tried to achieve that with my code below but the scores in table2.Total_Score never accumulate. They stay the same, even though my code gave no error. See my code below.
update table2 o inner join
(
SELECT op.StudentID,
sum(ot.Total_Score) as Total_Score_ot
sum(op.Total_Score) as Total_Score_op
FROM table1 ot inner join table2 op on op.StudentID = ot.StudentID
WHERE ot.Grade = 'Grade8'
GROUP BY op.StudentID
) as o1 on o.StudentID = o1.StudentID
SET Total_Score=Total_Score_op + Total_Score_ot
Hmmm . . . I don't think you need a join in the subquery:
update table2 o inner join
(select ot.StudentID, sum(ot.Total_Score) as Total_Score_ot
from table1 ot
where ot.Grade = 'Grade8'
group by ot.StudentId
) ot
using (studentid)
set o.Total_Score = o.Total_Score + Total_Score_ot;
Is anybody able to help me construct a query based on the schema in the attached image? I have been at this all morning but am hopeless with joins and cannot get it to work.
What I would like to be able to do is to select all rows from table 1 where table 4 ID referenced in table 1 is NOT available to the table 5 ID referenced in table 1.
Please let me know if that doesn't make sense, any help would be greatly appreciated!
I'm not completely sure that I ahve understood the question, but does this query fits your needs?
SELECT *
FROM Table1
WHERE Table1.Table4_ID NOT IN ( SELECT t1.Table5_ID
FROM Table1 as t1
)
I eventually figured this out. The query I needed was:
SELECT Table1.Table1_ID
FROM Table1
INNER JOIN Table5
ON Table1.Table5_ID = Table5.Table5_ID
INNER JOIN Table2
ON Table5.Table2_ID = Table2.Table2_ID
INNER JOIN Table3
ON Table2.Table2_ID = Table3.Table2_ID
INNER JOIN Table4
ON Table3.Table3_ID = Table4.Table3_ID
WHERE Table4.Table4_ID <> Table1.Table4_ID
AND Table1.Table5_ID = 5
I created a relatively simple query in MySQL to give me a JOIN on three tables based on where first names and last names matched. From there, I wanted to write another query that would then only show me the records that didn't get matched from the JOIN -- but I couldn't quite figure out how to do it. I'm assuming that it has to do with using a subquery involving something like NOT IN and my original query, but I couldn't get it to give me the results I wanted.
This is the work-around I tried to come up with that partially functioned properly:
SELECT *,
if(t2.first=t1.first AND t2.last=t1.last, "Match", "No Match") AS "t2 Match",
if(t3.first=t1.first AND t3.last=t1.last, "Match", "No Match") AS "t3 Match"
FROM t1
LEFT JOIN t2 ON t2.first=t1.first AND t2.last=t1.last
LEFT JOIN t3 ON t3.first=t1.first AND t3.last=t1.last
WHERE if(t2.first=t1.first AND t2.last=t1.last, "Match", "No Match")="No Match"
OR if(t3.first=t1.first AND t3.last=t1.last, "Match", "No Match")="No Match";
I feel like this is something that's fairly simple and straight-forward, but I'm not getting the correct results. Can anybody help?
Thanks!
No match means that the t2 (or t3) columns are populated with Nulls in the results. So you can use IS NULL checks:
SELECT t1.*
FROM t1
LEFT JOIN t2 ON t2.first = t1.first AND t2.last = t1.last
LEFT JOIN t3 ON t3.first = t1.first AND t3.last = t1.last
WHERE t2.first IS NULL
OR t3.first IS NULL ;
And you were right, you can also write the queries using NOT IN (warning: only if the joining columns are not nullable. Otherwise you may have unexpected results):
SELECT t1.*
FROM t1
WHERE (first, last) NOT IN
( SELECT first, last FROM t2 )
OR (first, last) NOT IN
( SELECT first, last FROM t3 )
;
or using NOT EXISTS:
SELECT t1.*
FROM t1
WHERE NOT EXISTS
( SELECT 1
FROM t2
WHERE t1.first = t2.first
AND t1.last = t2.last
)
OR NOT EXISTS
( SELECT 1
FROM t3
WHERE t1.first = t3.first
AND t1.last = t3.last
) ;
I'm working on this query on mySQL - it seems to be processed on a website with PEAR and MDB2 on the running server (dont know why… din't do that myself). On my local test system it generates always MDB2 error. PHPmyadmin doesn't do this query as well.
The Subselection is needed because there are not just one but four subselects in this query.
SELECT * FROM table1
WHERE table1.orderID
IN
(
SELECT *
FROM table1
LEFT JOIN table2
ON (table1.customID = table2.customID)
WHERE table1.active=1
)
I can simplify it like that (works):
SELECT * FROM table1
WHERE table1.orderID
IN (1,2,3)
The Subselect works, too:
SELECT *
FROM table1
LEFT JOIN table2
ON (table1.customID = table2.customID)
WHERE table1.active=1
Thank you so much for any help!
The inner query should return exactly one column, like:
SELECT *
FROM table1
WHERE table1.orderID IN
(
SELECT orderId
-- ^^ here
FROM table1
LEFT JOIN
table2
ON table1.customID = table2.customID
WHERE table1.active=1
)
Can anyone help me please? Inner join query working fine. but query displaying duplicate data. I don't to display duplicate data.
here is my query.
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
Here is output
"COLOR";"456";"456";"Nude"
"COLOR";"456";"456";"Ivory"
"COLOR";"456";"456";"Black"
"COLOR";"456";"456";"Coral"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Coral"
"COLOR";"459";"459";"Nude"
"COLOR";"459";"459";"Ivory"
"SIZE";"460";"460";"Large"
"SIZE";"460";"460";"Medium"
"SIZE";"460";"460";"Small"
"SIZE";"470";"470";"Large"
"SIZE";"470";"470";"Small"
"SIZE";"470";"470";"Medium"
"COLOR";"476";"476";"White"
"COLOR";"476";"476";"Black"
"SIZE";"477";"477";"Small"
But i don't to display duplicate data. for example which is displaying here.
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"459";"459";"Black"
"COLOR";"460";"60";"Black"
is there any way?? thanks
Maybe you just want to group it by names? You seem to be calling a duplicated data what seems to have different ids...
SELECT DISTINCT t1.class, t1.classid, t2.classid, t2.option_name
FROM table1 AS t1
INNER JOIN table AS t2 ON t1.classid = t2.classid
GROUP BY t1.class,t2.option_name