with this database I did the next query :
WITH comp_courses AS
(SELECT * FROM course WHERE title LIKE "Comp%")
SELECT * FROM (instructor INNER JOIN (comp_courses INNER JOIN teaches USING(course_id)) USING(ID));
now that returns a join of a bunch of tables. But what I want is only the colmuns from that query that appear in instructor. Any ideas on how to do it?
All you would have to do is select all the columns from that table using the * operator like so:
instructor.*
This should get you the result your looking for:
SELECT
instructor.*
FROM
instructor AS `i`
JOIN teaches AS `t` ON `t`.id = `c`.id
JOIN course AS `c` ON `c`.course_id = `t`.course_id
WHERE
`c`.title LIKE 'Comp%';
Hope this helps,
I try to make a query using the INNER JOIN command and it executes successfully but it yields no results.
Here is the SQL code I am using:
SELECT * FROM `departament` INNER JOIN `fakultet`
ON "departament.#ID_Fakultet"="fakultet.ID_Fakultet"
And here are the tables
the departament table:
the fakultet table
if you write in double (" ") or single (' ') quotes it will take as string.
SELECT * FROM `departament` INNER JOIN `fakultet`
ON departament.`#ID_Fakultet`=fakultet.`ID_Fakultet`
can you run this query?
I have a select statement that uses inner joins on multiple tables, and I want to get COUNT() from one particular table, however my current statement is throwing an error:
Syntax error: unexpected 'COUNT' (count)
Helpful. I know. Gotta love MySQL's detailed and in-depth error messages.
Here is my select statement:
SELECT SE.SEId, SE.ParentME, SE.ParentSE, SE.Name, SE.Status, SE.Description,
UDC.UDCId, UDC.Code, UDC.Description,
TRM.COUNT(*)
FROM SubEquipment SE
INNER JOIN UserDefinedCode UDC ON UDC.ETId = SE.EquipmentType
INNER JOIN Terminal TRM ON TRM.SEId = SE.SEId
GROUP BY TRM.SEId
WHERE ParentME = #MEId;
What am I doing wrong? Is this possible?
You want to do the following:
SELECT SE.SEId, SE.ParentME, SE.ParentSE, SE.Name, SE.Status, SE.Description,
UDC.UDCId, UDC.Code, UDC.Description,
COUNT(DISTINCT TRM.SEID)
FROM SubEquipment SE
INNER JOIN UserDefinedCode UDC ON UDC.ETId = SE.EquipmentType
INNER JOIN Terminal TRM ON TRM.SEId = SE.SEId
WHERE ParentME = #MEId
GROUP BY 1,2,3,4,5,6,7,8,9
Because Count is an aggregate your single measures must be grouped. Plus the error you're seeing is because COUNT isn't a column in TRM. That's what it thinks you're asking for.
Try COUNT(DISTINCT [the TRM primary key field(s)]); it should count the distinct terminal "id" values, so even if the intermediate JOIN multiples the rows, you'll still get the number of terminals.
In addition to FirebladeDan's answer, (as he suggested) a subquery also cleaned this issue up:
SELECT DISTINCT SE.SEId, SE.ParentME, SE.ParentSE, SE.Name, SE.Status, SE.Description,
UDC.UDCId, UDC.Code, UDC.Description,
--Subquery to get the count
(SELECT COUNT(*) FROM Terminal WHERE TRM.SEId = SE.SEId) AS TerminalCount
FROM SubEquipment SE
INNER JOIN UserDefinedCode UDC ON UDC.ETId = SE.EquipmentType
LEFT JOIN Terminal TRM ON TRM.SEId = SE.SEId
WHERE ParentME = #MEId;
This got rid of the need for grouping the columns.
Subnote: I changed the INNER JOIN on the Terminal table to a LEFT JOIN, because if a SEId did not have any associated terminals, it would not return any information, which also called for a DISTINCT query.
I have the following select statement:
select *
from ( select q.*,
qg.index_min as groupIndexMin,
qg.index_max as groupIndexMax,
fo.form_condition,
fo.form_array,fo.form_index
from question_group qg, question q
right join form fo on q.form_id = fo.form_id
where q.form_id=14 and
(q.question_group_id=qg.question_group_id and q.question_type_id NOT IN(12))
order by question_order) m
left join (select r.response_text,
r.response_id,
r.response_index,
f.*,
a.*
from response r
right outer join field f on r.field_id=f.field_id and r.response_index=5
right outer join application a on r.application_id=a.application_id
right outer join user u on a.user_id=u.user_id
where (a.application_id=70 and u.user_id=47)) n on m.field_id = n.field_id
order by m.question_order ;
Now I need,
Delete Query : for the above select query using same conditions the record will be delete where response_index=5.
Update Query : Suppose, if I delete response_index=5, then remaining response_index will be update. For example response_index=6 => response_index=5 and response_index=7 => response_index=6 like that.
Please help me.
I am using Hibernate in my application. Currently I am trying to execute the following query:
DELETE FROM ActiveTimes a WHERE
a.begin>=:from AND a.begin<=:to
AND a.end>=:from AND a.end<=:to
AND a in(
SELECT al FROM ActiveTimes al
JOIN al.source.stage st
JOIN st.level.dataSource ds
WHERE ds=:dataSource)
But I get an error: Column 'id' in field list is ambiguous.
This feels normal, because the created SQL query looks like this:
delete
from
active_times
where
begin>=?
and begin<=?
and end>=?
and end<=?
and (
id in (
select
id
from
active_times activeti1_
inner join
sources sourc2_
on activeti1_.source=sourc2_.id
inner join
stage stage3_
on sourc2_.id=stage3_.source
inner join
levels levels4_
on stage3_.level=levels4_.id
inner join
datasources datasource5_
on levels4_.data_source=datasource5_.id
where
id=?
)
)
If I change the query to:
DELETE FROM ActiveTimes a WHERE
a.begin>=:from AND a.begin<=:to
AND a.end>=:from AND a.end<=:to
AND a.id in(
SELECT al.id FROM ActiveTimes al
JOIN al.source.stage st
JOIN st.level.dataSource ds
WHERE ds.id=:dataSource)
I get another error: You can't specify target table 'active_times' for update in FROM clause.
I am not very experimented with JPQL(or HQL) so I do not understand why the query looks like that in the first example.
The new error occurs because apparently I cannot make a subquery on the delete table in MySQL.
Do you have any suggestions on how can I rewrite one of the above queries in order to make it work?
Just remove the sub-query. It's unnecessary. I'm not sure how to write SQL code in Hybernate, but I'm guessing it would be something like this:
DELETE a
FROM ActiveTimes a
JOIN a.source.stage st
JOIN st.level.dataSource ds
WHERE a.begin>=:from AND a.begin<=:to
AND a.end>=:from AND a.end<=:to
AND ds.id=:dataSource;