简体   繁体   English

使用非数字ID获取MYSQL中的上一个和下一个记录

[英]Get Previous and Next records in MYSQL with non-numeric ID

I am trying to develop a small photo cataloging and storage system in PHP / MySQL. 我正在尝试在PHP / MySQL中开发小型照片分类和存储系统。 Currently my database is structured as follows: 目前,我的数据库结构如下:

CREATE TABLE `photos` (
  `picid` varchar(36) NOT NULL,
  `uploaded` varchar(10) NOT NULL,
  `picdesc` text NOT NULL,
  `views` bigint(20) NOT NULL,
  `albumid` varchar(36) NOT NULL COMMENT 'fkey albums',
  `uploadedby` varchar(50) NOT NULL COMMENT 'fkey users',
  `exif` longtext NOT NULL,
  `album_protected` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`picid`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

The picid field is a GUID used as the primary key. picid字段是用作主键的GUID。

I need help with creating an SQL query that will return the record previous to a passed in picid and the record next from it too. 我在创建SQL查询时需要帮助,该查询将返回传入的picid之前的记录以及返回的下一个记录。 I think I will need to use two queries, but perhaps someone can tell me otherwise. 我想我将需要使用两个查询,但是也许有人可以告诉我。 The records are ordered by uploaded which is a UNIX TIMESTAMP value. 记录按上uploaded顺序排序,这是UNIX TIMESTAMP值。

Hope you can help! 希望能对您有所帮助! Please tell me if you require more info! 如果您需要更多信息,请告诉我!

Give it an auto_increment integer field as primary key, and change picid to just unique. 给它一个auto_increment整数字段作为主键,并将picid更改为唯一。 As unique, it will be indexed and can still be used as lookups. 作为唯一,它将被索引,并且仍然可以用作查找。 But if you want sequence in your rows, you need an auto_increment field and those have to be primary keys in MySql. 但是,如果要在行中排序,则需要一个auto_increment字段,并且这些字段必须是MySql中的主键。

 CREATE TABLE `photos` (
 `rowid` int auto_increment primary key,
 `picid` varchar(36) NOT NULL UNIQUE,
 `uploaded` varchar(10) NOT NULL,
 `picdesc` text NOT NULL,
 `views` bigint(20) NOT NULL,
 `albumid` varchar(36) NOT NULL COMMENT 'fkey albums',
 `uploadedby` varchar(50) NOT NULL COMMENT 'fkey users',
 `exif` longtext NOT NULL,
 `album_protected` tinyint(1) NOT NULL default '0'
 ) ENGINE=MyISAM DEFAULT CHARSET=utf8;

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

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