我当前的数据库看起来像
CREATE TABLE sites(
siteId INT(11) AUTO_INCREMENT,
siteType VARCHAR(255),
siteName VARCHAR(255),
siteDomain VARCHAR(255),
PRIMARY KEY(siteId)
);
CREATE TABLE siteSites(
parentId INT(11),
childId INT(11)
)
我正在尝试加入所有表并获取所有数据。 像:
<?php
$q=mysql_query("SELECT * FROM sites s1, siteSites, sites s2 WHERE s1.siteId=parentId AND s2.siteId=childId");
$row=mysql_fetch_array($q);
?>
并且我希望从$ row变量中获取's1'和's2'的信息。 这是可能的,如果它是我怎么做的?
谢谢
答案 0 :(得分:4)
SELECT s1.siteId as ParentSiteId, s1.siteType as ParentType, s1.siteName as ParentName, s1.siteDomain as ParentDomain,
s2.siteId as ChildSiteId, s2.siteType as ChildType, s2.siteName as ChildName, s2.siteDomain as ChildDomain
FROM sites s1
INNER JOIN siteSites ss
ON s1.siteId = ss.parentId
INNER JOIN sites s2
ON ss.childId = s2.siteId
答案 1 :(得分:1)
你的问题不是很明确......
如果我对它有任何意义,你想在一个查询中拉出整个图表。如果是这样,不,这在MySQL中是不可能的。这样做需要recursive with statement。
如果没有,并且您当前的查询是正确的,则需要对列名称进行别名而不是select *
,例如:
select s1.siteid as parent_id,
s1.sitename as parent_name,
...,
s2.siteid as child_id,
s2.sitename as child_name,
...
答案 2 :(得分:0)
我不知道我是否理解你的问题 试试这个:
SELECT s1.*, s2.*
FROM sites s1 JOIN siteSites ss
ON s1.siteId = ss.parentId
JOIN sites s2
ON ss.childId = s2.siteId
答案 3 :(得分:0)
你只缺少while循环:
while($row=mysql_fetch_array($q))
{
echo $row['siteName'];
echo $row['siteDomain'];
echo $row['parentId'];
// etc ..., access to values by field name
}