id title cat lang
6101 AAba 1 1
6102 AAad 4 2
6103 AAaf 4 2
6104 AAa 1 1
6105 AAtar 4 2
6106 AAao 1 1
6107 ABya 4 2
6108 ABar 4 2
6109 ACar 1 2
6110 BCad 3 1
6111 CCas 4 1
6112 DCas 4 2
6113 GCaz 2 2
6114 FCaew 2 2
6115 FCaw3 4 2
6116 FCa3 1 1
6117 FCa4 4 2
6118 FCa5 2 1
6119 FCa6 4 2 --- last id
Table news, id is primary key, title - is title field, cat - there are four types of categories of news, lang - language code 1 or 2.
I want to achieve following: 1) Case Edit news - id is available - for example 6111, lang is 1, cat is 4 I want to collect these id and titles:
6119
6117
6115
6112
6109
6105
6103
6102
which are the opposite language - lang is 2, the same category cat is 4 and within range of 4 (or n) id before and after given id 6111 also ordered desc
2) Case Add news - id is not available, or after insert in php is mysql_insert_id() the same functionality - last 10 id with the same cat = 4 and the opposite lang = 2
至今为止,我尝试过这一点:
复数 1) php 片段 :
$id = 6111;
$lang = 1;
$tip = 4;
$sql = "SELECT id, title
FROM pubs
WHERE id BETWEEN ".($id-4)." AND ".($id+4). " AND
tip = ".$tip." AND
lang <> ".$lang."
ORDER BY id DESC ";
< 坚固> BUT < / 坚固 > 这在 id 之前和之后得到 4 id, 而不是从我需要的 id 收集 。
案例2)
$sql = "SELECT id, title
FROM news
WHERE cat = 4 AND
lang <> 1
ORDER BY id DESC LIMIT 10";
这似乎有用,但也许我错了
Is there any way to achieve Case 1 this with php and mysql? Better way for case 2?