inner-join

Simple hql named query that uses a inner join

て烟熏妆下的殇ゞ 提交于 2019-12-05 07:30:47
问题 I want to do something like this in my domain/entity object : @Entity @NamedQueries({ @NamedQuery(name="favouriteCats", query="from Cat c inner join on UserCat uc where uc.isFavourtie = true and uc.user = :user") }) public final class Cat extends BaseTable So that in my service layer I can do this : Query query = session.getNamedQuery("favouriteCats") query.setParameter(0, MyUser); return query.list(); However, my syntax in HQL is incorrect - and aftern ten minutes looking at official docs I

Ambiguous column name SQL error with INNER JOIN: Why?

北战南征 提交于 2019-12-05 03:31:24
The following code will work to select data from two tables: SELECT t1.foo, t2.bar FROM TABLE1 t1 INNER JOIN TABLE2 t2 ON t1.foo=t2.foo I could just as easily written SELECT t2.foo, t2.bar FROM TABLE1 t1 INNER JOIN TABLE2 t2 ON t1.foo=t2.foo t1.foo or t2.foo : six of one or half a dozen of the other. Why not just foo ? I've been wonder why doesn't the SQL server just automatically return the data without me specifying one table or the other since the choice is entirely arbitrary (as far as I can tell). I can make up a scenario where you would need to specify the table, such as SELECT t1.foo,

Mysql range check instead of index usage on inner join

自闭症网瘾萝莉.ら 提交于 2019-12-05 01:41:39
I'm having a serious problem with MySQL (innoDB) 5.0. A very simple SQL query is executed with a very unexpected query plan. The query: SELECT SQL_NO_CACHE mbCategory.* FROM MBCategory mbCategory INNER JOIN ResourcePermission as rp ON rp.primKey = mbCategory.categoryId where mbCategory.groupId = 12345 AND mbCategory.parentCategoryId = 0 limit 20; MBCategory - contains 216583 rows ResourcePermission - contains 3098354 rows. In MBCategory I've multiple indexes (columns order as in index): Primary (categoryId) A (groupId,parentCategoryId,categoryId) B (groupId,parentCategoryId) In

Django Inner Join Queryset

China☆狼群 提交于 2019-12-04 23:28:11
I'm working with Django and I need to do a queryset using two inner joins. I have three models A, B, and C and I want to do a query like the following in psql: select DISTINCT a from A inner join B on B.a_id = A.id inner join C on C.b_id = B.id; Models: (only included relevant fields) class A(models.Model): id = models.IntegerField(primary_key=True) class B(models.Model): id = models.IntegerField(primary_key=True) a = models.ForeignKey(A, null=True, blank=True,on_delete=models.SET_NULL) class C(models.Model): b = models.ForeignKey(B, null=True, on_delete=models.SET_NULL) So everything in C

Order by in Inner Join

徘徊边缘 提交于 2019-12-04 22:47:08
I am putting inner join in my query.I have got the result but didn't know that how the data is coming in output.Can anyone tell me that how the Inner join matching the data.Below I am showing a image.There are two table(One or Two Table). According to me that first row it should be Mohit but output is different.Please tell me. Thanks in advance. dotariel You have to sort it if you want the data to come back a certain way. When you say you are expecting " Mohit " to be the first row, I am assuming you say that because " Mohit " is the first row in the [One] table. However, when SQL Server joins

How to do joins on subqueries in AREL within Rails

混江龙づ霸主 提交于 2019-12-04 18:28:24
问题 I have a simple model class User has_many :logs class Logs related in the usual way through the foreign key logs.user_id. I'm trying to do the following using Arel and according to the Arel doc it should work. u_t = Arel::Table::new :users l_t = Arel::Table::new :logs counts = l_t. group(l_t[:user_id]). project( l_t[:user_id].as("user_id"), l_t[:user_id].count.as("count_all") ) l_t.joins(counts).on(l_t[:id].eq(counts[:user_id])) When I do that I get the error TypeError: Cannot visit Arel:

Conditional inner join statements in MySQL

纵然是瞬间 提交于 2019-12-04 16:39:28
Is there a way I can conditionally change which table i'm inner joining on based on the value of a field in another table? Here's what I got so far (but it errors) out: SELECT j.jobID, j.jobNumber, CASE WHEN j.idType = 'dealership' THEN d.dealershipName WHEN j.idType = 'Group' THEN g.groupName WHEN j.idType = 'Agency' then a.agencyName END as dealershipName, CASE WHEN p.manualTimestamp != '0000-00-00 00:00:00' THEN UNIX_TIMESTAMP(p.manualTimestamp) WHEN p.manualTimestamp = '0000-00-00 00:00:00' THEN p.timestamp END as checkTS, CONCAT_WS(' ', ui.fName, ui.lName) as salesRep FROM jobs j LEFT

MySQL query for summing values in another table

别等时光非礼了梦想. 提交于 2019-12-04 16:03:46
I have got a couple of tables with a parent child relationship. I want to apply a sum function to one column of child table and return it with all data of parent table for example. Parent_table ID, Date, Title Child_table ID, another_id, column_to_sum //(ID is foreign key pointing to Parent_table) Sample Data in Parent_table 1, 22-11-2010 00:00:00 , 'Some Title' 2, 13-11-2010 00:00:00 , 'Some Title 2' Sample Data in Child_table 1, 1, 10 1, 2, 11 1, 8, 3 2, 5, 11 2, 8, 6 Output of the query should return all columns in parent_table with an additional column, that is, summing the values of

inner join with empty result from right table

≯℡__Kan透↙ 提交于 2019-12-04 11:57:15
I have 2 tables, restaurants and orders, each restaurant can have many orders restaurants table id name orders table id restaurant_id date I need to find the restaurants that have no orders on some date range. In orders table I save the order dates like - each row represents one day. So, I need to make inner join, but with no results from the orders table. Say, I need to find restaurants that are free from 2013-08-09 to 2013-08-11 date range. How can I achieve this ? How to make a query, that will give the restaurants with no matching in the orders table - according to the date range ?

How to join two tables based on substring values of fields?

倾然丶 夕夏残阳落幕 提交于 2019-12-04 09:49:48
I am having problem with sql. I want to join two tables, employee and class instructor. Condition is that employee is having unid column like 'u0871457' where as class instructor is having EmplId as '00871457'. I just want to replace the first character of EmplId to 'u' to join to match the string coming from unid . How can I do that? I have tried this so far : select e.name, i.name from Employee e inner join Instructor i on SUBSTR(e.id,1, LENGTH(e.id )) = SUBSTR(i.id,1, LENGTH(i.id )) but this is resulting into a blank resultset. Any help will be appreciated. Thanks for your time! So many