简体   繁体   English

显示内部联接的数据

[英]displaying data from inner join

i need help displaying this in two different lists 我需要帮助在两个不同的列表中显示它

$domains_sql = mysql_query("SELECT domains_id, domains_url, keywords_id, keywords_word, domains_comments_comment

     FROM
     (
     SELECT domains_id,domains_url
     FROM domains
     ORDER BY RAND()
     LIMIT 1
     ) as d

     INNER JOIN domains_comments
     ON domains_comments_domain = domains_id

     INNER JOIN domains_keywords
     ON domains_keywords_website = domains_id

     INNER JOIN keywords
     ON domains_keywords_keyword = keywords_id
         ORDER BY keywords_word ASC") or die (mysql_error());


$num = mysql_num_rows($domains_sql);

$current_price = "";


for($i=0;$i<$num;$i++){


$domains_result = mysql_fetch_array($domains_sql);

$domains_id = $domains_result['domains_id'];
$domains_url = $domains_result['domains_url'];
$domains_name = preg_replace('#^https?://www.#', '', $domains_url);                

$keywords_id = $domains_result['keywords_id'];
$keywords_word = $domains_result['keywords_word'];

$domains_comments = $domains_result['domains_comments_comment'];

if($domains_url != $current_price) {

echo $domains_name."<br /><br />";

$current_price = $domains_url;

}

echo $keywords_word."<br />";
echo $domains_comments."<br />";
}

prints out: 打印出:

MS Office 微软Office
domain 1 域1
MS Office 微软Office
domain 1 - part 1 域1-第1部分
MySQL MySQL的
domain 1 域1
MySQL MySQL的
domain 1 - part 1 域1-第1部分
PHP PHP
domain 1 域1
PHP PHP
domain 1 - part 1 域1-第1部分
Visual Basic Visual Basic
domain 1 域1
Visual Basic Visual Basic
domain 1 - part 域1-部分

and i need it to be: 我需要它是:

(info from keywords) (来自关键字的信息)

MS Office 微软Office
MySQL MySQL的
PHP PHP
Visual Basic Visual Basic

(info from comments) (评论信息)

domain 1 域1
domain 1 - part 1 域1-第1部分

I'm not sure if I see how your domain comments and keywords are related, and if you simply want two lists or if you want one comment-list per keyword. 我不确定是否看到您的域注释和关键字之间的关系,是否只需要两个列表,或者每个关键字只需要一个注释列表。

Anyway, you could rewrite your query logic to make a primary query for the keywords, and then run simpler queries for the comments (either one for all, or one per keyword), but if your query time and dataset is such that you prefer doing it in a single query you can restructure the data in multiple-leve arrays. 无论如何,您可以重写查询逻辑以对关键字进行一次主查询,然后对注释运行更简单的查询(一次查询全部或每个关键字一次),但是如果您的查询时间和数据集如此,则您希望这样做在单个查询中,您可以重组多级数组中的数据。

Also, i presume you only want to list every item once. 另外,我想您只想列出每个项目一次。

$keywords = array();
$comments = array(); //if you want all comments in one list
for($i=0;$i<$num;$i++){
  $domains_result = mysql_fetch_array($domains_sql);

  //Stores complete row data, only keeps the last, if the same value is fetched from the db several times
  $keywords [$domains_result['keywords_word']] = $domains_result;
  $comments[$domains_result['domains_comments_comment']] = $domains_result;
}
//Now you have the two list, could be printed several ways, to only print the values
print implode("<br />\n",array_keys($keywords));
print "<br />";
print implode("<br />\n",array_keys($comments));

//Loop trough
foreach ($keywords as $keyword=>$data) {
  print "$keyword<br>\n";
  print $data['keywords_word']."<br>\n";
  print_r($data);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM