[英]How to declare a child table as parent table in a sql query?
I am working on a search page and there are total 5 table I have to join.我正在搜索页面上工作,我必须加入总共 5 个表。
Table one (fields) = id | title | country | state
Table two (fields) = id | category
Table three (fields) = id | tbl_1_id | tbl_2_id | value
Table Four (fields) = id | tbl_1_id | data
Table Five (fields) = id | tbl_1_id | tags
The problem is, my query cannot fetch data from Table two
My query is,问题是,我的查询无法从
Table two
获取数据我的查询是,
SELECT one.*, two.*, three.*, four.*, five.*
FROM one
LEFT JOIN two ON two.id = three.tbl_2_id
LEFT JOIN three ON one.id = three.tbl_1_id
LEFT JOIN four ON one.id = four.tbl_1_id
LEFT JOIN five ON one.id = five.tbl_1_id
WHERE
one.title LIKE '%$keyword%' OR
one.country LIKE '%$keyword%' OR
one.state LIKE '%$keyword%' OR
two.category LIKE '%$keyword%' OR
three.value LIKE '%$keyword%' OR
four.data LIKE '%$keyword%' OR
five.tags LIKE '%$keyword%' ORDER By one.id DESC";
I think, Table two
don't have any field from Table one
that is why my query not getting data from Table two
Am I right?我认为,
Table two
没有Table one
中的任何字段,这就是为什么我的查询没有从Table two
获取数据的原因对吗?
Table one
is the parent table and all other tables are child. Table one
父表,所有其他表都是子表。 Right?对? What I thought was, if I can make
Table two
another parent and Table three
it's child, maybe my query will work.我的想法是,如果我可以让
Table two
另一个父Table three
,而Table three
是子Table three
,也许我的查询会起作用。
I am looking for a query, what can get all data from all table.我正在寻找一个查询,可以从所有表中获取所有数据。 Whether the columns are empty or not.
列是否为空。 Hope you guys understand.
希望大家理解。 If not, please let me know, Thanks.
如果没有,请告诉我,谢谢。
I solved it.我解决了。 :) This is my new query -
:) 这是我的新查询 -
SELECT one.*, two.*, three.*, four.*, five.*
FROM one
LEFT JOIN two
INNER JOIN three ON two.id = three.tbl_2_id
ON one.id = three.tbl_1_id
LEFT JOIN four ON one.id = four.tbl_1_id
LEFT JOIN five ON one.id = five.tbl_1_id
WHERE
one.title LIKE '%$keyword%' OR
one.country LIKE '%$keyword%' OR
one.state LIKE '%$keyword%' OR
two.category LIKE '%$keyword%' OR
three.value LIKE '%$keyword%' OR
four.data LIKE '%$keyword%' OR
five.tags LIKE '%$keyword%' ORDER By one.id DESC
I don't know if it's the right way to write this query, but it worked.我不知道这是否是编写此查询的正确方法,但它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.