gpt4 book ai didi

MySQL : How to query two tables sequentially?

转载 作者:行者123 更新时间:2023-11-29 10:38:14 24 4
gpt4 key购买 nike

例如,
有两个表articles_1articles_2,它们具有相同的字段:

id
title
content
slug

当有一个表时,我可以这样查询一篇文章:

public function show($slug)
{
$article = Article::findOrFail($slug);

return view('show', compact('article'));
}

现在有两个表,我想查询一篇文章,代码怎么写?

最佳答案

你可以使用 UNION

$table1_items = \DB::table('articles_1')
->select(\DB::raw("id, title, content,slug"))

$table2_items = \DB::table('software_items')
->select(\DB::raw("id, title, content,slug"))

$results = $table1_items->union($table2_items)->get();

关于MySQL : How to query two tables sequentially?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46022745/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com