MyBatis Dynamic SQL join on subquery - join

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

Related

Hive join query to list columns from only one table

I am writing a hive query to join two tables; table1 and table2. In the result I just need all columns from table1 and no columns from table2.
I know the solution where I can select all the columns manually by specifying table1.column1, table1.column2.. and so on in the select statement. But I have about 22 columns in table 1. Also, I have to do the same for multiple other tables ans its painful process.
I tried using "SELECT table1.*", but I get a parse exception.
Is there a better way to do it?
Hive 0.13 onwards the following query syntax works:
SELECT a.* FROM a JOIN b ON (a.id = b.id)
This query will select all columns from a. So instead of typing all the column names (making the query cumbersome), it is a better idea to use tablealias.*

Nested queries. Select as column

Is it possible to write this query in ROR?
SELECT column_1,
(SELECT name FROM table_2 WHERE table_2.column_1 = table_1.column_1) as name
FROM table_1;
Yes, it is possible:
Table_1.select("column_1, (SELECT name FROM table_2 WHERE table_2.column_1 = table_1.column_1) as name")
If you will user Arel it will seems yet more complicated then this.
But exists other ways to simplify this query:
split it to two query and merge it together in Rails
using joins method for join table_1 and table_2 and select field table_2.name.

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;

JPQL join two entities with no direct relations

I have an issue: When I am trying to join two tables which do not have a foreign key or a direct entity relation through my java code within themselves. I am using the below JPQL query: -
SELECT p FROM P p, OM orgm WHERE p.o.id = orgm.o.id and p.u.id = orgm.u.id and orgm.ma = true and p.u.id = ? AND p.o.id IN (:oId);
But this turns to a MySQL query which has a "cross join" which obviously is expensive.
What I need is to make sure that a similar query gives me an inner join MySQL query between the two tables.
I am trying to make usage of the "WITH" clause but seems that it doesn't work with inner join.
Please revert what can be done in this scenario.
Thanks in advance.

Left join in SubSonic Problem

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()

Resources