Left join in SubSonic Problem - join

I'm a beginner in SubSonic and I'm using version 2.1. I'd like to perform a left join in this query. The query looks like:
select ...
from tableA
left join tableB on tableA.Cola=tableB.Colb and tableB.Colc='some value'
I want to know how to perform the and tableB.Colc='some value' condition. I tried something like this:
new SubSonic.Select().From("tableA").LeftOuterJoin
("tableB","Colb","tableA","Cola").AndExpression("Colc").IsEqualTo("some value")
but the generated statement is not what I wanted.

This may not be exactly what you want , but the best way to do things like this in subsonic is with views, So create the select as a view and then use the view object in your code. In 3+ the linq makes it alot easier to accomplish what you are trying

It looks to me like the and part of your query should really be a where condition (you're trying to filter your results based on the value of tableB.Colc being equal to 'some value'). So what I think your sql query should look like is:
select ...
from tableA left join tableB on tableA.Cola=tableB.Colb
where tableB.Colc='some value' or tableB.Colc is null
If that is the case then in SubSonic you would do:
new SubSonic.Select()
.From("tableA").LeftOuterJoin ("tableB","Colb","tableA","Cola")
.Where("Colc").IsEqualTo("some value")
.Or("Colc").IsNull()

Related

MyBatis Dynamic SQL join on subquery

I want to do something like this in MyBatis Dynamic SQL:
SELECT id FROM foo
JOIN (SELECT foo_id ...) bar ON foo.id = bar.foo_id
WHERE ...
However, the join() function only accepts SqlTable as an argument.
Is it possible to join on a subquery with MyBatis Dynamic SQL? If so, how do I do it?
MyBatis Dynamic SQL doesn't support these types of sub-queries right now. I'll think about adding it.
What's your database?
Do you really need to explicitly use JOIN like this?
Can't you do it just ike this?
SELECT id FROM foo , (select foo_id...) bar
WHERE foo.id = bar.foo_id

INNER JOIN on a table witch do not have any data

I'm trying to use the INNER JOIN functionality in my phpMyAdmin.
My query looks like this:
SELECT * FROM ___Bookings INNER JOIN ___Origins ON ___Bookings.BOO_Origin=___Origins.ORI_Id WHERE BOO_Id=1.
The problem is at this step nothing is populated into the ___Origins table. So my query returns 0 row.
How to change my query to return a row even if I do not have the joined table populated ?
Also what's the difference between JOIN and INNER JOIN ?
Thanks so much.
Basically, you want to join the tables based on the data in the __BOOKINGS table. That's a job for LEFT JOIN, not INNER JOIN (which is the same as JOIN).
Refer here for more information on SQL Joins: http://www.sql-join.com/sql-join-types/
There isn't a difference between Join and Inner Join (you can search for it also if this isn't enough).
For the other part - how can it return a row when there isn't one?
If you need a response from PHP you can set something like
if ($query->rowCount() > 0) {
echo $records;
}
else {echo "N";}
And in you other code just state if the response is "N" (or something else) do something or not do anything.

How to use SphinxSE table + sort by "weight desc" when there are other joins in query?

By default, when you perform query to sphinx table, Sphinx engine returns rows which are already sorted by query weight and does it really fast.
So, when I do this:
select
article.name
from article
left join article_ft on article._id=article_ft.id
where article_ft.query='some text;mode=any;';
Where:
article is InnoDB like table.
article_ft is Sphinx table.
Both of them (article.name and article_ft) contain these data (1 line = 1 row):
This is text.
This is also some text.
This is another text.
Sphinx engine will return rows like:
This is also some text.
This is text.
This is another text.
But, If I do something like this:
select
article.name
from article
left join article_ft on article._id=article_ft.id
left join article_category on article.category=article_category._id
where article_ft.query='some text;mode=any;';
It seems, MariaDB sorts it by its own way here.
Even If I provide Sphinx's 'sort' option like this:
select
article.name
from article
left join article_ft on article._id=article_ft.id
left join article_category on article.category=article_category._id
where article_ft.query='some text;mode=any;sort=extended:#weight desc;';
Still it doesn't work.
Changing order of joins doesn't work as well.
If I use order by article_ft.weight DESC MariaDB returns error message like:
Error: ER_ILLEGAL_HA: Storage engine SPHINX of the table `article_ft` doesn't have this option
in case if article has no rows that could match condition like article.category=50.
article_ft was created using this:
CREATE TABLE article_ft
(
id BIGINT NOT NULL,
weight INTEGER NOT NULL,
query VARCHAR(3072) NOT NULL,
INDEX(query)
) ENGINE=SPHINX CONNECTION="sphinx://192.168.1.98:9402/article_ft";
How to use this "magical" sort by weight feature if query contains more joins with no errors in return?
Thanks forward, for any reply!
P.S. Can't provide you a fiddle for this because I do not know any SQL fiddle online service which supports Sphinx Tables. Also if you found more relevant topic question I'll appreciate that.
Put the article_ft table first in the query. ie ... article_ft inner join article ...
Or maybe use FORCE INDEX, to force the use of the query index. Then it might honour the sort order.
Failing that use a subquery?
(select name,weight from article_ft ... ) order by weight desc;

HQL Query fails when using join and with clause

Good Evening,
I am using Grails and I am trying to do an HQL Query
I have an object Opportunity and inside it an object Entity and inside the Entity a collection of Titles. Each Title object can be main or not (main is a boolean field that shows which of the titles is the default one). So the query that I am doing is this:
select opportunity from Opportunity as opportunity join opportunity.entity.titles as entityTitle with entityTitle.isMain is true
But this query fails with this message:
org.hibernate.hql.ast.InvalidWithClauseException: with-clause expressions did not reference from-clause element to which the with-clause was associated.
I have tried adding the Entity and Title tables and still it fails. If I remove the with clause it works correctly but I need to filter the titles.
Thanks.
Try this query:
select distinct op from Opportunity as op
inner join op.entity as ent
inner join ent.titles as tit
with tit.isMain is true
I found the problem. First I had to join the entity to the Opportunity and then join the titles to the Opportunity. So the query is like this:
select opportunity from Opportunity as opportunity
join opportunity.entity activeEntity
with activeEntity.isActive is true
join activeEntity.titles entityTitle
with entityTitle.isActive is true

executing query from ruby on rails the right way

I'm just beginning with ruby on rails and have a question regarding a bit more complex query. So far I've done simple queries while looking at rails guide and it worked really well.
Right now I'm trying to get some Ids from database and I would use those Ids to get the real objects and do something with them. Getting those is a bit more complex than simple Object.find method.
Here is how my query looks like :
select * from quotas q, requests r
where q.id=r.quota_id
and q.status=3
and r.text is not null
and q.id in
(
select A.id from (
select max(id) as id, name
from quotas
group by name) A
)
order by q.created_at desc
limit 1000;
This would give me 1000 ids when executing this query from sql manager. And I was thinking to obtain the list of ids first and then find objects by id.
Is there a way to get these objects directly by using this query? Avoiding ids lookup? I googled that you can execute query like this :
ActiveRecord::Base.connection.execute(query);
Assuming Quota has_many :requests,
Quota.includes(:requests).
where(status:3).
where('requests.text is not null').
where("quotas.id in (#{subquery_string_here})").
order('quotas.created_at desc').limit(1000)
I'm by no means an expert but most basic SQL functionality is baked into ActiveRecord. You might also want to look at the #group and #pluck methods for ways to eliminate the ugly string subquery.
Calling #to_sql on a relationship object will show you the SQL command it is equivalent to, and may help with your debugging.
I would use find_by_sql for this. I wouldn't swear that this is exactly right, but as I recall you can pretty much plonk an SQL statement into a find_by_sql and the resulting columns will be returned as attributes of an array of objects of the class you call it on:
status = 3
Quota.find_by_sql('
select *
from quotas q, requests r
where q.id=r.quota_id
and q.status= ?
and r.text is not null
and q.id in
(
select A.id from (
select max(id) as id, name
from quotas
group by name) A
)
order by q.created_at desc
limit 1000;', status)
If you come to Rails as someone used to writing raw SQL, you're probably better off using this syntax than stringing together a bunch of ActiveRecord methods - the result is the same, so it's just a matter of what you find more readable.
Btw, you shouldn't use string interpolation (i.e. #{variable} syntax) inside an SQL query. Use the '?' syntax instead (see my example) to avoid SQL injection potential.

Resources