inner-join

Subqueries for each result or inner join with php loop or …?

浪子不回头ぞ 提交于 2019-12-14 04:05:39
问题 I want to display in users search results also their photos. Not only avatar photo but 3 photos. Photos are in extra table user_photos. If I would get single row per user the answer would be clear - inner join. But I will get multirows for each user. First method I can use is join: SELECT * FROM users INNER JOIN photos ON photos.user_id=users.user_id In this case I need some extra php code to merge results with same user_id. Something like this: foreach($results as $result){ if(isset($user[

query linking 4 tables

走远了吗. 提交于 2019-12-14 04:00:00
问题 I have 4 tables at present: Users: UserID (PK) FName SName Pic Posts: ReportID (PK) UserID Title Description Pic Comments: CommentID (PK) UserID PostID UserSubscriptions: ID (PK) UserID PostID isRead I need to retrieve all comment details for all posts a user is subscribed to (where there is a comment available). This query is used to populate a my activity feed page in an app. When a user makes a post, they are subscribed to that post and when a user comments on a post, they are subscribed

SQL Server performance - Subselect or Inner Join?

↘锁芯ラ 提交于 2019-12-14 03:51:11
问题 I've been pondering the question which of those 2 Statements might have a higher performance (and why): select * from formelement where formid = (select id from form where name = 'Test') or select * from formelement fe inner join form f on fe.formid = f.id where f.name = 'Test' One form contains several form elements, one form element is always part of one form. Thanks, Dennis 回答1: The performance depends on the query plan choosen by the SQL Server Engine. The query plan depends on a lot of

django query with …objects.raw

感情迁移 提交于 2019-12-14 03:16:57
问题 How I can make a query with two models using this model.objects.raw(...) and into the sql query has the INNER JOIN with the another model(table) this is possible model.objects.raw(' SELECT establecimiento.nombre, categoria.titulo FROM establecimiento INNER JOIN categoria ON establecimiento.categoria = categoria.id') I need print the establecimiento's name with his categoria's name class Establecimiento(models.Model): nombre = models.CharField(max_length = 140) categoria = models.ForeignKey

one sql command with two connection string

我与影子孤独终老i 提交于 2019-12-14 01:11:46
问题 I want to run this query in C# SELECT * FROM [FirstDataBase].[dbo].[table1] INNER JOIN [SecondDataBase].[dbo].[table2] and my code is : SqlConnection cn = new SqlConnection(myConnectionString); SqlCommand cmd = new SqlCommand(@"SELECT * FROM [FirstDataBase].[dbo].[table1] INNER JOIN [SecondDataBase].[dbo].[table2]"); cmd.Connection = cn; // here is my question !!! cn.Open(); int x = (int)cmd.ExecuteScalar(); but my query needs two connection string ... one for [FirstDataBase] and second for

Conditionally fallback to different join condition if stricter condition not matched

人走茶凉 提交于 2019-12-13 22:38:24
问题 I have 2 tables j and c. Both tables have columns ports and sec, and JOIN ON j.ports = c.ports and c.sec = j.sec. For j.port = 'ABC' , if there is no c.sec = j.sec for the same ports, then JOIN ON LEFT(c.sec, 6) = LEFT(j.sec, 6) For other j.ports , I only want to join ON j.ports = c.ports and c.sec = j.sec How can I do that? Example Data Table c +------+------------+------------+ | Port | sec | Other | +------+------------+------------+ | ABC | abcdefghij | ONE | | ABC | klmnop | TWO | | LMN

Joining between multiple databases

会有一股神秘感。 提交于 2019-12-13 21:24:44
问题 Update I get an Invalid object name error for the DataBaseA.dbo.templates I wish to Inner join two tables from seperate databases on the same server (Local). But i can't seem to get it work. Also i don't understand how i should be handling my connectionstring, as i'm currently setting the current connection to the connectionstring of my database. But in this example i'm trying to connect to two different databases in the same query. This is my SQL code so far: "SELECT a.template.*, b.c

Find string position and join another table's row

余生颓废 提交于 2019-12-13 21:18:52
问题 I have two main questions on getting 2 set of tables. table : room_type rt_id rt_title 1 Type A 2 Type B 3 Type C 4 Type D 5 Type E Here is another table to come with. table: rate_cost id rate hotel cost date 2999 7 1 4700-5400-6100-6600-7300 2012-11-01 3000 7 1 4700-5400-6100-6600-7300 2012-11-02 3001 7 1 4700-5400-6100-6600-7300 2012-11-03 3002 7 1 4700-5400-6100-6600-7300 2012-11-04 3003 7 1 4700-5400-6100-6600-7300 2012-11-05 3004 7 1 4700-5400-6100-6600-7300 2012-11-06 3005 7 1 4700-5400

Yii2: What is the correct way to define relationships among multiple tables?

元气小坏坏 提交于 2019-12-13 18:53:33
问题 In a controller I have the following code: public function actionView($id) { $query = new Query; $query->select('*') ->from('table_1 t1') ->innerJoin('table_2 t2', 't2.t1_id = t1.id') ->innerJoin('table_3 t3', 't2.t3_id = t3.id') ->innerJoin('table_4 t4', 't3.t4_id = t4.id') ->andWhere('t1.id = ' . $id); $rows = $query->all(); return $this->render('view', [ 'model' => $this->findModel($id), 'rows' => $rows, ]); } See the db schema: https://github.com/AntoninSlejska/yii-test/blob/master

Pandas inner merge/join returning all rows

时光怂恿深爱的人放手 提交于 2019-12-13 18:29:45
问题 I'm trying to merge two data frames based on a column present in both, keeping only the intersection of the two sets. The desired result is: foo bar foobar x y z x j i x y z j i a 1 2 a 9 0 a 1 2 9 0 b 3 4 b 9 0 b 3 4 9 0 c 5 6 c 9 0 c 5 6 9 0 d 7 8 e 9 0 f 9 0 My code that does not produce the desired result is: pd.merge(foo, bar, how='inner', on='x') Instead, the code seems to return: foo bar foobar x y z x j i x y z j i a 1 2 a 9 0 a 1 2 9 0 b 3 4 b 9 0 b 3 4 9 0 c 5 6 c 9 0 c 5 6 9 0 d 7