从2表中选择并订购它们

时间:2015-07-25 19:07:08

标签: php mysql

首先,抱歉我的英文...
我想要的是从两个SQL表中选择,然后按照特定的顺序制作它们,比如在论坛中...... 我有两个表,主题和用户,我想从他们两个中选择一个推杆作者信息旁边他的主题
这是主题和用户的SQL

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button class="btnIncFont">+</button>
<div class="parentDiv">
  <div style="display:inline-block;max-width:120px"><span>This is a test1</span>
  </div>
  <div style="display:inline-block;max-width:120px"><span>This is a test2</span>
  </div>
  <div style="display:inline-block;max-width:120px"><span>This is a test3</span>
  </div>
  <div style="display:inline-block;max-width:120px"><span>This is a test4</span>
  </div>
</div>

并且php代码可能看起来像这样

CREATE TABLE IF NOT EXISTS `topics` (
  `id` int(11) NOT NULL,
  `id2` int(11) NOT NULL,
  `title` varchar(256) NOT NULL,
  `message` longtext NOT NULL,
  `author_id` int(11) NOT NULL,
  `timestamp` int(11) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `users` (
  `id` bigint(20) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `avatar` varchar(255) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

有什么办法吗?

2 个答案:

答案 0 :(得分:3)

据我所知,您正在寻找的是要执行的正确SQL语句。以下简单的解决方案。

<?php
    $sql = mysql_query('SELECT users.username, topics.message FROM `users` INNER JOIN topics ON topics.author_id = users.id');
    while($row = mysql_fetch_array($sql)) {
        echo '<p>'.$row['username']'<br>';
        echo $row['message'].'<br></p>';
    }
?>

答案 1 :(得分:0)

SELECT * FROM `users` INNER JOIN topics ON topics.author_id = users.id'