初心者です。一度似た質問をさせていただきましたが、再度整理しましたので、質問に答えて頂けたら幸いです。
下記のm_magazine(DBのテーブル名)の中にそれぞれの月のmagazine_id(1、2、3…と番号順に登録してあります。)と月刊のurl、タイトルであるmagazine_titleなどのカラムを収納させてあります。
生成させた今月号の月刊ページには来月号のリンクと先月号のリンクを自動的に取得させたいのですが、下記のように書くと、最新号は次のようになってしまします。
「
次の月刊へ:2016年2月号 ○○○ 前の月刊へ:2016年1月号 ○○○
次の月刊へ:2016年3月号 ○○○ 前の月刊へ:2016年2月号 ○○○
次の月刊へ:2016年4月号 ○○○ 前の月刊へ:2016年3月号 ○○○
」
つまり、最新号には過去の全部の「次の月刊へ」と「前の月刊へ」が全部出てしまいます。
次に新しい号は「次の月刊へ:2016年4月号 ○○○ 前の月刊へ:2016年3月号 ○○○」は出ずに、あとは全部出てしまいます。最も古い号は逆に何も出ません。
どなたか教えて頂けませんでしょうか。よろしくお願い致します。
◆DB仕様
「
-- Table "m_magazine" DDL
CREATE TABLE `m_magazine` (
`magazine_id` int(11) NOT NULL COMMENT '月刊ID',
`url` varchar(256) DEFAULT NULL COMMENT 'URL',
` magazine_datetitle` varchar(256) DEFAULT NULL COMMENT '月刊年月タイトル',
`magazine_title` varchar(256) DEFAULT NULL COMMENT '月刊タイトル',
`is_deleted` char(1) NOT NULL COMMENT '削除フラグ(y, n)',
PRIMARY KEY (`magazine_id`),
KEY `is_deleted` (`is_deleted`),
KEY `create_staff_id` (`create_staff_id`),
KEY `modify_staff_id` (`modify_staff_id`),
CONSTRAINT `m_magazine_ibfk_1` FOREIGN KEY (`create_staff_id`) REFERENCES `m_staff` (`staff_id`),
CONSTRAINT `m_magazine_ibfk_2` FOREIGN KEY (`modify_staff_id`) REFERENCES `m_staff` (`staff_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='月刊管理マスタ';
」
◆生成に使っているPHPファイル
「
$currentid = $h['magazine_id'];
$sql2 = "select distinct url, magazine_datetitle, magazine_title from m_magazine where magazine_id = (select min(magazine_id) from m_magazine where magazine_id > '$currentid' order by magazine_id desc limit 1)";
$sql3 = "select distinct url, magazine_datetitle, magazine_title from m_magazine where magazine_id = (select max(magazine_id) from m_magazine where magazine_id < '$currentid' order by magazine_id desc limit 1)";
$ret2 = f_db_select($sql2, $h['url'], $h['magazine_datetitle'], $h['magazine_title']);
$ret3 = f_db_select($sql3, $h['url'], $h['magazine_datetitle'], $h['magazine_title']);
foreach ($ret2 as $h2) {
$nexturl = f_db_select($h2['url']);
$nextdatetitle = f_db_select($h2['magazine_datetitle']);
$nexttitle = f_db_select($h2['magazine_title']);
}
foreach ($ret3 as $h3) {
$prevurl = f_db_select($h3['url']);
$prevdatetitle = f_db_select($h3['magazine_datetitle']);
$prevtitle = f_db_select($h3['magazine_title']);
$wkbuff .= <<<EOF
<p>
次の月刊へ:<a href ="{$h2['url']}">{$h2['magazine_datetitle']} {$h2['magazine_title']}</a><br/>
前の月刊へ:<a href ="{$h3['url']}">{$h3['magazine_datetitle']} {$h3['magazine_title']}</a>
</p>
EOF;
」
A 回答 (1件)
- 最新から表示
- 回答順に表示
No.1
- 回答日時:
一律で次や前を書くことはできないことは理解できてますか?
(例外処理が必要とかきましたよね?)
とりあえず、なんのCMSかわかりませんがビミョーな感じ
たとえばこんな風にidを抜き出してみては?
CREATE TABLE `m_magazine` (
`magazine_id` int(11) NOT NULL PRIMARY KEY,
`url` varchar(256) not null,
`magazine_title` varchar(256) not null,
`is_deleted` enum('y','n') not null default 'n'
);
insert into `m_magazine` values
(1,'url1','title1','n')
,(2,'url2','title2','n')
,(4,'url4','title4','n')
,(5,'url5','title5','y')
,(6,'url6','title6','n')
,(7,'url7','title7','n');
条件:
・idが必ずしも連番とは限らない
・削除フラグが'y'のモノは無視する
select
(select max(`magazine_id`) from `m_magazine` as t1 where t1.`magazine_id`<t0.`magazine_id` and t1.`is_deleted`='n') as pre_id
,t0.`magazine_id` as `this_id`
,(select min(`magazine_id`) from `m_magazine` as t2 where t2.`magazine_id`>t0.`magazine_id` and t2.`is_deleted`='n') as nxt_id
from `m_magazine` as t0
where t0.`is_deleted`='n';
これにurlとタイトルを拡張して
select `pre_id`,t4.`url` as `pre_url`,t4.`magazine_title` as `pre_title`
,`this_id`,`this_url`,`this_title`
,`nxt_id`,t5.`url` as `nxt_url`,t5.`magazine_title` as `nxt_title`
from (
select
(select max(`magazine_id`) from `m_magazine` as t1 where t1.`magazine_id`<t0.`magazine_id` and t1.`is_deleted`='n') as pre_id
,t0.`magazine_id` as `this_id`
,t0.`url` as `this_url`
,t0.`magazine_title` as `this_title`
,(select min(`magazine_id`) from `m_magazine` as t2 where t2.`magazine_id`>t0.`magazine_id` and t2.`is_deleted`='n') as nxt_id
from `m_magazine` as t0
where t0.`is_deleted`='n'
) as t3
left join `m_magazine` as t4 on `pre_id`=t4.`magazine_id`
left join `m_magazine` as t5 on `nxt_id`=t5.`magazine_id`
仮に自ページのidが4なら、t0のmagazine_idで絞って
select
`pre_id`
,t4.`url` as `pre_url`
,t4.`magazine_title` as `pre_title`
,`this_id`
,`this_url`
,`this_title`
,`nxt_id`
,t5.`url` as `nxt_url`
,t5.`magazine_title` as `nxt_title`
from (
select
(select max(`magazine_id`) from `m_magazine` as t1 where t1.`magazine_id`<t0.`magazine_id` and t1.`is_deleted`='n') as pre_id
,t0.`magazine_id` as `this_id`
,t0.`url` as `this_url`
,t0.`magazine_title` as `this_title`
,(select min(`magazine_id`) from `m_magazine` as t2 where t2.`magazine_id`>t0.`magazine_id` and t2.`is_deleted`='n') as nxt_id
from `m_magazine` as t0
where t0.`is_deleted`='n'
and t0.`magazine_id`=4
) as t3
left join `m_magazine` as t4 on `pre_id`=t4.`magazine_id`
left join `m_magazine` as t5 on `nxt_id`=t5.`magazine_id`
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- その他(ホビー) GLOCK19Gen5 と GLOCK19Gen5 MOS とで、マガジンの重さが10gも違うのは? 1 2022/05/08 16:20
- エアガン・モデルガン 4.2 lb = 1905.09 g のはずなのが、 (⇩)下記の URL の SPECS の所の 1 2022/10/06 15:43
- 洋画 「GLOCK 19 Gen5」 の マガジン と 「GLOCK 19 Gen5 MOS」の マガジン 1 2023/05/16 19:14
- PHP php テーブルが作成できない 1 2022/11/17 23:41
- MySQL php テーブルを作れない 2 2022/11/17 18:22
- PHP コメント機能に返信欄を矢印で追加したい 1 2022/05/09 21:17
- MySQL テーブル作成です。どこかのスペルが間違っているか記号など スペースかな? 1 2022/10/01 05:08
- JavaScript 1度きりではなく、繰り返し、挙動が変わるようにしていただきたいです。 1 2022/07/03 15:50
- PHP php my adminより取り出したデータ表示 2 2022/06/15 11:56
- MySQL my_itemsテーブルのIDにAUTO_INCREMENT を追加ができるかで 1 2023/01/03 09:09
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
エクセルで最後の文字だけ置き...
-
[MySQL] 3つのテーブルの結合で...
-
エクセルの関数について教えて...
-
sqlで、600行あるテーブルを100...
-
SQLサーバから、項目の属性(型...
-
上位3位を求めるSQL文は?
-
副問合せの書き方について
-
SQL Left Join で重複を排除す...
-
SQLにて特定の文字を除いた検索...
-
[MySQL] UNIQUE制約の値を更新...
-
Access パラメータクエリをcsv...
-
selectした大量データをinsert...
-
親と子供が複数のSQL取得方法
-
テーブルが5つの時の結合の仕...
-
書籍の内容はまともでしょうか?
-
クエリ表示と、ADOで抽出したレ...
-
【SQL文】Insert into文で文法...
-
VIEWの元のテーブルのindexって...
-
inner joinをすると数がおかし...
-
Mysqlでunionを使った検索速度...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
エクセルで最後の文字だけ置き...
-
SQL Left Join で重複を排除す...
-
SQLサーバから、項目の属性(型...
-
副問合せの書き方について
-
VIEWの元のテーブルのindexって...
-
エクセルの関数について教えて...
-
SQLにて特定の文字を除いた検索...
-
マイクラPC版のコマンドで効率...
-
select文のwhere句に配列を入れ...
-
sqlで、600行あるテーブルを100...
-
ある条件の最大値+1を初番する...
-
inner joinをすると数がおかし...
-
Unionした最後にGROUP BYを追加...
-
期間の重複を調べるSQL文につい...
-
クエリ表示と、ADOで抽出したレ...
-
Access パラメータクエリをcsv...
-
PL/SQLの変数について
-
MySQLのDATE型カラム値がNULLの...
-
php+mysqlで複数選択削除について
-
上位3位を求めるSQL文は?
おすすめ情報