2015-09-04 102 views
0

我需要根據其他表獲取表中最後一個ID。返回唯一ID

更清楚,這是我的實際工作(測試)http://pepeok.com/plugins/combined2.php

所以這段代碼工作做好,並返回所有的數據,但我需要以這種方式得到的只是最後的ID: 的title_id = UNIQUE & LAST

當然,在這種情況下,title_id是電影或tvShow,所以我想在最後一部電影中只有1個鏈接,或者在電視劇的最後一集中。

這是我的實際代碼:

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT l.id, l.label, l.title_id, t.title, t.poster, l.season, l.episode, l.approved FROM links l JOIN titles t ON l.title_id = t.id WHERE approved = 1 order by id desc LIMIT 30 OFFSET 1"; 
$result = $last_id = $conn->query($sql); 

if ($result->num_rows > 0) 
{ 

    // output data of each row 
    while($row = $result->fetch_assoc()) { 
//THE CONTENT GO HERE ---- 
} else { 
    echo "0 results"; 
} 
$conn->close(); 

注: 這個結果是基於2個表 - 標題&鏈接,獨有的ID是在桌子上「標題」(ID)和錶鏈接(title_id的)

表結構 - 鏈接:

CREATE TABLE IF NOT EXISTS `links` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    `url` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `type` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'embed', 
    `label` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `title_id` bigint(20) unsigned DEFAULT NULL, 
    `season` int(10) unsigned DEFAULT NULL, 
    `episode` int(10) unsigned DEFAULT NULL, 
    `reports` int(10) unsigned NOT NULL DEFAULT '0', 
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `temp_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `approved` tinyint(1) NOT NULL DEFAULT '1', 
    `positive_votes` int(11) NOT NULL DEFAULT '0', 
    `negative_votes` int(11) NOT NULL DEFAULT '0', 
    `quality` varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'SD', 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `links_url_unique` (`url`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=53281 ; 

個表結構標題:

CREATE TABLE IF NOT EXISTS `titles` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    `title` varchar(255) COLLATE utf8_unicode_ci NOT NULL, 
    `type` varchar(15) COLLATE utf8_unicode_ci NOT NULL DEFAULT 'movie', 
    `imdb_rating` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `tmdb_rating` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `mc_user_score` varchar(3) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `mc_critic_score` smallint(5) unsigned DEFAULT NULL, 
    `mc_num_of_votes` int(10) unsigned DEFAULT NULL, 
    `imdb_votes_num` bigint(20) unsigned DEFAULT NULL, 
    `release_date` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `year` smallint(5) unsigned DEFAULT NULL, 
    `plot` text COLLATE utf8_unicode_ci, 
    `genre` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `tagline` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `poster` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `background` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `awards` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `runtime` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `trailer` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `budget` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `revenue` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `views` bigint(20) NOT NULL DEFAULT '1', 
    `tmdb_popularity` float(50,2) unsigned DEFAULT NULL, 
    `imdb_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `tmdb_id` bigint(20) unsigned DEFAULT NULL, 
    `season_number` tinyint(3) unsigned DEFAULT NULL, 
    `fully_scraped` tinyint(3) unsigned NOT NULL DEFAULT '0', 
    `allow_update` tinyint(3) unsigned NOT NULL DEFAULT '1', 
    `featured` tinyint(3) unsigned NOT NULL DEFAULT '0', 
    `now_playing` tinyint(3) unsigned NOT NULL DEFAULT '0', 
    `created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, 
    `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `temp_id` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `language` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `country` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `original_title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `affiliate_link` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL, 
    `custom_field` text COLLATE utf8_unicode_ci, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `titles_imdb_id_unique` (`imdb_id`), 
    UNIQUE KEY `titles_tmdb_id_type_unique` (`tmdb_id`,`type`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=2106587 ; 

非常感謝您的幫助。

+0

,因爲你需要你的其他結果爲好,使用'while',然後使用PHP的'end',獲得數組的最後一個元素的結果存儲在陣列中。 – CodeGodie

回答

1

嘗試

SELECT 
l.id, l.label, l.title_id, t.title, t.poster, l.season, l.episode, l.approved 
FROM 
links l , titles t , (SELECT max(l.id) as 'thelastid' FROM links l) t2 
WHERE 
approved = 1 AND l.title_id = t.id AND t2.thelastid=l.id 
+0

您好,請根據我的示例編寫完整的代碼,因爲我沒有得到它的工作。 - 非常感謝。 – aladin2222

+0

@ aladin2222再試一次 –

0
$sql = <<<'sql' 
    SELECT 
     DISTINCT l.id, 
     l.label, l.title_id, t.title, t.poster, l.season, 
     l.episode, l.approved 
    FROM 
     links l 
    JOIN titles t 
     ON l.title_id = t.id 
    WHERE approved = 1 
    order by 
     id DESC 
    LIMIT 1 
    OFFSET 1 
sql; 

應返回查詢的最後一個不同的結果。不完全確定OFFSET適用於您的查詢,因此不確定是否需要。

+0

OFFSET是跳過第一個結果(我現在已經刪除),但添加「DISTINCT」不幫助(返回相同的結果)。現在我有這個 - $ sql =「SELECT DISTINCT l.id,l.label,l.title_id,t.title,t.poster,l.season,l.episode,l.approved FROM links l JOIN titles t ON l.title_id = t.id WHERE approved = 1 order by id desc LIMIT 30「; – aladin2222

+0

這是結果 - http://pepeok.com/plugins/combined2.php - – aladin2222

+0

我現在讀了DISTINCT,所以這將消除重複的行,但需要連接 - t.id的結果 - (表格標題),因爲這是標題(電影)的ID並且是唯一的。其他 - l.id - (錶鏈接)僅用於鏈接到電影,所以1 TITLE(電影)可以有很多鏈接(l.id),我只需要返回最後一個。 – aladin2222