English 中文(简体)
Mysql 内ner 加入不同的表格
原标题:mysql Inner join with different table

感谢大家: ()我有4个表格项目、要点、步骤和评论;

Project id name desc

points id name desc project_id

steps id desc points_id

comments id desc steps_id

我像这样写了查询

SELECT * FROM Project 
INNER JOIN points ON points.project_id=Project.id
INNER JOIN steps ON steps.points_id=points.id
INNER JOIN comments ON comments.steps_id=steps.id
WHERE Project.id=333

one project has many points, points, has many steps, and it has many comments somthing like this, and i whant to get all results in one query otherwise it takes a lot of time get results :(( i have no idea what can i do to :((

是这样子的

**Project**
id :1,
name :"get",

**points**
id :1,
name :"points1", ///project "get" s point
project_id : 1,
id :2,
name :"points2", ///project "get" s point
project_id : 1,

**steps**
id :1,
name :"steps1", ///project "points2" s step
points_id : 2,
id :2,
name :"steps2",///project "points2" s step
points_id : 2,

**comments**
id :1,
name :"something", ///project "steps1" s comment
steps_id : 1,
id :2,
name :"something",///project "steps2" s comment
steps_id : 2,

) ) )))(如果有其他办法解决这个问题?谢谢支持 :())))))

问题回答

首先,您有一个无效的 sql 语法。 您错过了关键字 < code> FROM 。 它应该是 :

SELECT * 
FROM Table1
       INNER JOIN table2 ON table2.table1_id= table1.id
       INNER JOIN table3 ON table3.table2_id= table2.id
       INNER JOIN table4 ON table4.table3_id= table3.id
WHERE table1.ID = 333

但我需要你的预期结果。 您需要指定您的数据库图案和假记录来完成查询 。

在您张贴的查询中遗漏了几件东西 :

  1. Missing FROM clause
  2. Also, every join is not correct like table2.table1_id= table1.id ... it should be table2.table2_id= table1.id (Unless your table design is like so)

所以,最终你的查询应该看起来像

SELECT *  FROM Table1        
INNER JOIN table2 ON table2.table2_id= table1.id        
INNER JOIN table3 ON table3.table2_id= table1.id        
INNER JOIN table4 ON table4.table4_id= table1.id 
WHERE table1.ID = 333 

您正在通过以 333 来过滤 ID 来从表1 中获取所有列。 从其它表格中再获取一列如何? 您期待什么?

最后一个INNER JOIN中有一个打字机。 表2应该是表4。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签