0

我有两张桌子。一个保存用户状态内容,另一个保存用户。如何编写用户查询以获取他们在流数据中给定流项目的用户记录?目前,它只抓取位于用户表顶部的用户。

$check = "SELECT streamitem_id, streamitem_timestamp, streamitem_content FROM streamdata WHERE streamitem_creator="$user1_id." AND streamitem_id=".$last." AND streamitem_type_id=1 ORDER BY streamitem_timestamp DESC";
$check1 = mysqli_query($mysqli,$check);
$resultArr = mysqli_fetch_array($check1);
$json['streamitem_id'] = $resultArr['streamitem_id'];
$json['streamitem_timestamp'] = Agotime($resultArr['streamitem_timestamp']);
$json['streamitem_content'] = $resultArr['streamitem_content'];
mysqli_free_result($check1);


$check = "SELECT * FROM users";
$check1 = mysqli_query($mysqli,$check);
$resultArr = mysqli_fetch_array($check1);
$json['username'] = $resultArr['username'];
$json['id'] = $resultArr['id'];
$json['first'] = $resultArr['first'];
$json['middle'] = $resultArr['middle'];
$json['last'] = $resultArr['last'];
mysqli_free_result($check1);


-- Table structure for table `streamdata`
--

    CREATE TABLE IF NOT EXISTS `streamdata` (
      `streamitem_id` int(11) NOT NULL auto_increment,
      `streamitem_type_id` int(11) NOT NULL,
      `streamitem_creator` int(11) NOT NULL,
      `streamitem_target` int(11) NOT NULL,
      `streamitem_timestamp` datetime NOT NULL,
      `streamitem_content` varchar(5000) NOT NULL,
      PRIMARY KEY  (`str

    eamitem_id`)
        ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1953 ;


    -- Table structure for table `users`
    --

    CREATE TABLE IF NOT EXISTS `users` (
      `id` int(11) NOT NULL auto_increment,
      `first` varchar(64) NOT NULL,
      `middle` varchar(64) NOT NULL,
      `last` varchar(64) NOT NULL,
     `username` varchar(64) NOT NULL,
      PRIMARY KEY  (`id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=150 ;
4

1 回答 1

1

查询以获取给定流项目的用户详细信息和流详细信息:

$check = "
SELECT * FROM users u 
INNER JOIN streamdata s ON
    (s.streamitem_creator = u.id AND s.streamitem_id = {$last})";

请注意,将原始变量值插入查询是不好的做法。特别是如果它们来自用户输入。查看mysqli_real_escape_string或尝试使用准备好的语句。

于 2012-08-30T11:54:58.277 回答