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
So far I have tried this:
for case 1) php snippet:
$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 this gets 4 id before and after the id, not from the collection of id I need .
for case 2)
$sql = "SELECT id, title
FROM news
WHERE cat = 4 AND
lang <> 1
ORDER BY id DESC LIMIT 10";
This seem to be working, but may be I’m wrong.
Is there any way to achieve Case 1 this with php and mysql?
Better way for case 2?
Given that you only have the
$id, this horrible looking beast is the best I can come up with. I’m fairly sure there’s a tidier way to do it, but I think this will work (FIXED)