inner-join

How to use Eager Loading and Join in Eloquent at a same time in laravel 4

痴心易碎 提交于 2019-12-11 01:09:30
问题 Models class WPModel extends Eloquent { protected $table = 'work_processes'; protected $guarded = array('id'); protected $softDelete = true; // declaring one-to-many relationship public function relatedWPAQs() { return $this->hasMany('WPAQModel', 'wp_id'); } public function relatedUsers() { return $this->belongsTo('UserModel', 'wp_owner_id'); } } class UserModel extends Eloquent { protected $table = 'users'; protected $softDelete = true; public function relatedWPs() { return $this->hasMany(

How could I rewrite a query without using joins

依然范特西╮ 提交于 2019-12-11 00:19:46
问题 I would like to know how this query would be written if no joins were used. I been trying to figure it out for cases where joins aren't viable or can't be used(aren't available). SELECT * FROM ( table1 INNER JOIN table2 ON table1.id = table2.id ) INNER JOIN table3 ON ( table1.id2 = table3.id2 ) AND ( table1.id3 = table3.id3 ) WHERE table1.id = 1 The reason I can't use joins is because the application uses HQL as opposed to standard SQL and HQL makes joins incredibly difficult to do. 回答1: It

Limit results on an GROUP_CONCAT() or INNER JOIN

百般思念 提交于 2019-12-10 20:44:06
问题 I've perused extensively the other threads talking about limits on group_concat() and inner joins but haven't found my answer, so I guess I'll go ahead and ask it: I'm developing an existing photo community site. I want to retrieve members who have their birthday on a given day (today) and then retrieve each member's 5 most highly rated photos. But I also only want the 10 "most favorite" birthday members (ie with the highest favorite count). Here's what I have: SELECT users.user_id, users

SQLite natural join broken?

非 Y 不嫁゛ 提交于 2019-12-10 19:48:22
问题 I am just getting to know NATURAL JOIN and SQLite is not behaning as I expect. SELECT * FROM r1 NATURAL JOIN (r2 NATURAL JOIN r3); and SELECT * FROM (r1 NATURAL JOIN r2) NATURAL JOIN r3; produce the same (correct) results. However if I omit the parentheses as in: SELECT * FROM r1 NATURAL JOIN r2 NATURAL JOIN r3; I see that r1 and r2 are joined correctly however r3 is not joined to the result at all, instead the cartesian product of r1 NATURAL JOIN r2, r3 is formed. Is there an issue with the

MySQL Show Null Results in Query - With INNER JOIN

依然范特西╮ 提交于 2019-12-10 17:06:15
问题 I have the following query: SELECT services.name as Service, services.logo_name as Logo, packages.name as Package FROM `client_services` INNER JOIN services ON service_id = services.id INNER JOIN packages ON packages.id = package_id WHERE client_id = 1 ORDER BY services.sort_id Well in client_services I have 5 results that need to be shown. 2 of them are NULL for package_id. When I run the query, it only shows 3 results, the ones that have a set package_id. If there is no package, I just want

Duplicated results when performing INNER JOIN

可紊 提交于 2019-12-10 16:48:24
问题 I have 2 simple tables that I would like to perform an INNER JOIN with, but the problem is that I'm getting duplicated (for the columns str1 and str2) results: CREATE TABLE #A (Id INT, str1 nvarchar(50), str2 nvarchar(50)) insert into #A values (1, 'a', 'b') insert into #A values (2, 'a', 'b') CREATE TABLE #B (Id INT, str1 nvarchar(50), str2 nvarchar(50)) insert into #B values (7, 'a', 'b') insert into #B values (8, 'a', 'b') select * from #A a INNER JOIN #B b ON a.str1 = b.str1 AND a.str2 =

MySQL query - Inner join using only the latest version of an entry

China☆狼群 提交于 2019-12-10 15:53:34
问题 I have a table, named jobs with various pieces of information. Each job is given a job number (unique id). There is then another table, named purchaseOrders that has a FK of jobID and a PK of poID. when a purchase order entry is edited, the old information is saved... meaning, i create a new PO entry (new unique id). What i'm trying to do, is write one query that selects all fields from "jobs" and all fields from "purchaseOrders" but only the latest poID for that job . For example: jobID Name

How To Join Two Tables In Nhibernate

╄→尐↘猪︶ㄣ 提交于 2019-12-10 15:22:49
问题 List<object[]> olist = null; olist = (_session.CreateQuery("Select pc.Id as Id,pct.DescEn as DescEn,pct.DescAr as DescAr,pc.ContentEn as ContentEn,pc.ContentAr as ContentAr from ProjectCharter pc,ProjectCharterTemplate pct where pct.Id=pc.PRC_PCT_ID and pc.PRC_PRJ_ID=1").List<object[]>()).ToList<object[]>(); This is My Query, I want to join two tables and get an output, when i run this is the db i get the perfect answere, but when i run it through c# with nhibernate mapping. i get errors. Can

delete from 3 tables with one query

感情迁移 提交于 2019-12-10 15:14:40
问题 i have 3 tables and i dont want define any foreign key in my tables. my tables structure are like below: tables diagram i have written this query : delete relativedata, crawls, stored from relativedata inner join crawls on relativedata.crawl_id = crawls.id and relativedata.id = ? inner join stored on stored.crawl_id = crawls.id this query works for me unless one of tables has no records. now how can i do this delete in 3 tables in 1 query? 回答1: If it works if all tables have records, try

Using GROUP BY and ORDER BY on an INNER JOIN SQL query

三世轮回 提交于 2019-12-10 15:06:10
问题 I am using the following query to group work times and expenses for clients from three tables, one for clients, one for work times and one for expenses: SELECT a.*, COALESCE(b.totalCount, 0) AS CountWork, COALESCE(b.totalAmount, 0) AS WorkTotal, COALESCE(c.totalCount, 0) AS CountExpense, COALESCE(c.totalAmount, 0) AS ExpenseTotal FROM clients A LEFT JOIN ( SELECT Client, COUNT(*) totalCount, SUM(Amount) totalAmount FROM work_times WHERE DATE BETWEEN '2013-01-01' AND '2013-02-01' GROUP BY