problem with nested inner joins in SQLIte - sqlite

The sql statement below will not run in SQLite:
select *
from A
left join (B inner join C on B.fkC = C.pk) on A.optionalfkB = B.pk
I get a sqlException "unknown column B.pk"
According to the documentation # http://www.sqlite.org/lang_select.html this should work, and it will work in all other sql implementations. Am I doing something wrong?

It doesn't work because the "outer" query doesn't know what B is.
select *
from A
left join (B inner join C on B.fkC = C.pk) B on A.optionalfkB = B.pk
The (B inner join C on B.fkC = C.pk) is weird without any select, but the specification does say that it is valid.

Related

sqlite query to get instances were no items returned on join

I am querying the static backend db for a game I play (trying to keep up on my coding), and I am having an issue getting the full results that I want.
So the query that I have so far is:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID == mc.constellationID
join mapRegions as mr on ms.regionID == mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
join invTypes as it on it.typeID == ii.typeID
where it.groupID = 9
group by solarSystemName
the problem comes when there are no rows where it.groupID == 9. What I need is for the count to return 0 and I can't for the life of me figure out how to get this to work.
I tried doing left outer join on the final join statement, but no joy.
Change the last join to a left join and set the condition in the on clause istead of the where clause:
select ms.security, mc.constellationName, mr.regionName, ms.solarSystemName, count(it.typename) as NumberOfBelts
from mapSolarSystems as ms
join mapConstellations as mc on ms.constellationID = mc.constellationID
join mapRegions as mr on ms.regionID = mr.regionID
join invItems as ii on ii.locationID = ms.solarSystemID
left join invTypes as it on it.typeID = ii.typeID and it.groupID = 9
group by solarSystemName

Doctrine DQL with multiple joined tables

i have entities **target,citytarget,city,cityplace,place
citytarget & cityplace are pivot tables that connect t&c and c&p
i need the places for given city name and a target id
i tried the following DQL:
SELECT t,c,p FROM MulticatorBundle:Target t
join t.cities ct
join ct.cities c
join c.places cp
join cp.places p
where c.name like '%Stahmeln%'
But i receive:
result: The parent object of entity result with alias 'c' was not found. The parent alias is 'ct'.
i dont know any further....
a plain SQL could be:
select * from target
left join citytarget on citytarget.target_id = target.id
left join city on citytarget.city_id = city.id
left join cityplace on cityplace.city_id = city.id
left join place on cityplace.id = place.id
where target.id = 1
and city.name like \'%Stahmeln%\'
Adrian
You always need your parent entity in your select. In this case:
SELECT ct, c, cp, t, p
is needed
You need to do
SELECT t,ct, c, cp ,p FROM MulticatorBundle:Target t
join t.cities ct
join ct.cities c
join c.places cp
join cp.places p
where c.name like '%Stahmeln%'
Hope this help you

Proc sql to Pull data from different tables recursively

I need to perform a recursive count operation on tables but here are the challenges that I am facing with.
Lets say I have tables A, B, C, D, E, F, .... Z
Here is the code snippet of what I have,
Proc sql;
create table temp as(
select count(*)
from a
inner join b on a.id = b.id
inner join c on a.id = c.id
inner join d on a.id = d.id
where <condition>
);
Once this code is complete I need to run the same query with B, C, D and E and update the result in same temp table that I am trying to create. This way I have to do for the entire table list that I have.
Is there a recursive sql to do this. I don't require a separate macro to call the query each time with different tables.
I would not do it quite this way.
proc sql;
create table temp as (
select count(case when n(a.id,b.id,c.id,d.id)=4 then 1 else 0 end) as abcd_count,
count(case when n(b.id,c.id,d.id,e.id)=4 then 1 else 0 end) as bcde_count
from a outer join b on a.id=b.id
outer join c ... etc.
;
quit;
IE, just do one join and use case when... to determine what has the counts you need. Here I use n() to identify records with all 4 ids on them.

Drupal db_query get all rows

I'm starting to build themes for Drupal.
I have a question about drupal queries, but I didn't find any answer...
I have a db_query in one of my modules, but only return a few rows (13 rows always, and table has 20) How can I select all rows from one table?
My query is like this
$result = db_query("SELECT * FROM bv_calendar c INNER JOIN bv_countries p ON c.country_id = p.country_id INNER JOIN bv_vaccinate v ON c.vaccinate_id = v.vaccinate_id ORDER BY $orderby ASC");
while ($class = $result->fetchAssoc()) {
$classes[$class["calendar_id"]] = $class;
}
How can I get all rows?
Thank you
From the looks of it I'd say you're not getting all the rows from the *bv_calendar* because you're using INNER JOIN on your SQL query which requires to be matching records on your *bv_countries* and *bv_vaccinate* tables.
Try replacing the INNER JOIN for LEFT JOIN as in the later "If there is no matching row for the right table in the ON or USING part in a LEFT JOIN, a row with all columns set to NULL is used for the right table." from http://dev.mysql.com/doc/refman/5.0/en/join.html.
If that's the result you want then your SQL query should be:
SELECT * FROM bv_calendar c LEFT JOIN bv_countries p ON c.country_id = p.country_id LEFT JOIN bv_vaccinate v ON c.vaccinate_id = v.vaccinate_id ORDER BY $orderby ASC

join issue, what are the best alternatives?

Q:
I have 5 or 6 tables , and i need data from these tables :
i make the following join statement to accomplish my query.
SELECT d.batch_no,d.studytype_code,d.camp_code,d.dep_code,d.start_date,d.end_date,a.year,a.term_code,c.studytype_name ,e.dep_name_ar,f.camp_name_ar
FROM llkbatch a , llkbatch_category b , mm19studytype c ,llkbatch_exception d ,llkdepartment e,llkcamp f
WHERE a.batch_no = b.batch_no
AND b.studytype_code = c.studytype_code
AND b.batch_no = d.batch_no
AND b.studytype_code = d.studytype_code
AND d.dep_code = e.dep_code
AND d.camp_code = f.camp_code
but i think that the join sometimes is less performance way to do things like that,is there any alternative to this in a programming way or in the database layer, i mean alternatives to joins in general and when should i go away from the joins.
thanks a lot.
There is a more modern ANSI join syntax that will make your joins more readable, but it should not affect performance:
SELECT d.batch_no,d.studytype_code,d.camp_code,d.dep_code,d.start_date,d.end_date,
a.year,a.term_code,c.studytype_name ,e.dep_name_ar,f.camp_name_ar
FROM ra1batch a
JOIN ra1batch_category b ON a.batch_no = b.batch_no
JOIN ra1studytype c ON b.studytype_code = c.studytype_code
JOIN ra1batch_exception d ON b.batch_no = d.batch_no
AND b.studytype_code = d.studytype_code
JOIN rr1department e ON d.dep_code = e.dep_code
JOIN rr2camp f ON d.camp_code = f.camp_code
The query you have written in your question follows the syntax of Oracle. But you use joins as if you are using SQL Server. It will give you better performance in comparison with your current query.
The below query will parse faster compared to your current query.
SELECT d.batch_no, d.studytype_code, d.camp_code, …
FROM ra1batch a
INNER JOIN ra1batch_category b on a.batch_no = b.batch_no

Resources