简体   繁体   中英

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. 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.

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. 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.

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. 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.

 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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