[英]How to declare a child table as parent table in a sql query?
我正在搜索页面上工作,我必须加入总共 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
问题是,我的查询无法从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";
我认为, Table two
没有Table one
中的任何字段,这就是为什么我的查询没有从Table two
获取数据的原因对吗?
Table one
父表,所有其他表都是子表。 对? 我的想法是,如果我可以让Table two
另一个父Table three
,而Table three
是子Table three
,也许我的查询会起作用。
我正在寻找一个查询,可以从所有表中获取所有数据。 列是否为空。 希望大家理解。 如果没有,请告诉我,谢谢。
我解决了。 :) 这是我的新查询 -
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
我不知道这是否是编写此查询的正确方法,但它有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.