簡體   English   中英

如何從 MySQL 中的另一個表中檢索數據,同時從第一個表的列中獲取數據?

[英]How to retrieve data from another table in MySQL while having a data from column from the first table?

在開始之前,我使用的是 Laravel 8 和 PHP 7.4.29。
我想要達到的效果是您可以根據第一個表(視頻)中的列從另一個表(用戶)中獲取用戶名。

讓我們仔細看看第一張桌子。 它包含諸如titledescriptionuploader (具有用戶的 UUID 值)等列。我面臨的問題是我實際上無法檢索我想要的用戶名。 而不是,我收到這樣的錯誤:

Trying to get property 'username' of non-object

代碼是:

(代碼編寫正確,但堆棧溢出語法突出顯示被破壞,我猜)

<?php
$videos = DB::select("select * from videos where title like concat(?) or description like concat(?) and public = ?", ["%".$_GET['query']."%", "%".$_GET['query']."%", 2]);
    foreach ($videos as $video) {
        $users = DB::select("select * from users where uuid = ?", [$video->uploader]);
?>
<div class="video">
    <a href="/video?id={{{ $video->videoid }}}">
        <div class="row">
            <div class="col-12 col-md-3">
                <div class="thumbnail">
                    <!-- the value below is a placeholder so far -->
                    <span class="duration-mark">4:51</span>
                </div>
            </div>
            <div class="col-12 col-md-9 my-2 my-md-0">
                <h4 class="mb-1">{{{ $video->title }}}</h4>
                <p class="viddesc mb-2">{{{ $users->username }}} ● {{{ date('Y/m/d', strtotime($video->uploaddate)); }}}</p>
                <p class="viddesc">{{{ $video->description }}}</p>
            </div>
        </div>
    </a>
</div>
<?php
    }
?>

將值分配給新變量也不起作用。

不久前我瀏覽了 Stack Overflow 並找到了解決方案,我應該在一個 SQL 請求中從兩個表中檢索數據(在我的情況下 - select * from videos, users )。 它以某種方式起作用,但不是預期的結果(我在第一個表中有 1 個視頻信息,在第二個表中有 2 個用戶),結果是重復的。

那時我實際上有一些使用 PHP 和 Laravel 的經驗,但我不記得如何做到這一點(我從 PHP 編程中獲得了相當大的突破)。 如果有任何解決方案,將受到歡迎。

提前致謝!

它不是從 laravel 視圖訪問模型的好方法。 您應該在控制器中執行此操作。 但是,您應該使用 get() 獲取多條數據,使用 first() 方法獲取單個數據以獲取實際數據對象。 你的代碼應該看起來像

<?php
$videos = DB::select("select * from videos where title like concat(?) or description like concat(?) and public = ?", ["%".$_GET['query']."%", "%".$_GET['query']."%", 2])->get();
foreach ($videos as $video) {
    $users = DB::select("select * from users where uuid = ?", [$video->uploader])->first();

?>

使用聯接

在您的情況下,如果您有沒有匹配上傳者的視頻並且需要所有視頻,請使用左連接。

如果您只想在表中存在匹配的上傳者時獲取視頻,您也可以使用內部聯接。

select videos.*,users.* from videos LEFT JOIN users ON users.uuid = videos.uploader where title like concat(?) or description like concat(?) and public = ?", ["%".$_GET['query']."%", "%".$_GET['query']."%", 2] 

參考: https ://www.w3schools.com/sql/sql_join_left.asp

注意:如果您在兩個表中都有一個公共列,則視頻記錄的值將被用戶列值覆蓋

例如:視頻 [id,name,uploader, addedAt] 用戶 [uuid, name]

 in this case the result will be 


    videos.id,videos,uploader,videos.addeAt,users.uuid,users.name
* The videos.name column willbe overrided by users.name

要克服這個問題,請像這樣使用上面的查詢

(僅示例)

select videos.*,users.*,videos.name as "videoname" from videos LEFT JOIN users ON users.uuid = videos.uploader where title like concat(?) or description like concat(?) and public = ?", ["%".$_GET['query']."%", "%".$_GET['query']."%", 2] 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM