Laravel从多个表获取数据

发布于 2025-02-09 10:55:28 字数 2226 浏览 1 评论 0原文

我有一个项目表,具有以下列的值:

id_project   name           website         country_id  currency_id  timezone_id
==========.  ====           =======         ==========. ===========. ===========
1            project name.  www.yahoo.com.  10          8            10

noundert 表具有以下列的价值:

id_country.      country_name
==========.      ============
10               America

a crentrencies 表具有以下列的值:

id_currency      currency_name
===========      ============
8                BD

时区表具有以下列的值:

id_timezone      time_onze    country_name
===========      =========    ============
10               Asia/Dhaka   Bangladesh

现在,我想运行一个应该给我 name 的查询,网站 country_name time_zone currency_name 。但是,您可以看到 country_name 都存在于 country 时区。因此,查询应该给我 country_name 来自 noundert 表而不是 time_zones 表。

因此,为此,我在查询之后正在运行:

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->get(['name', 'website', 'code', 'projects.currency_name', 'time_zone']);

return response()->json($projects, 200);

但是无法获取数据

错误消息:

“ sqlstate [42S22]:找不到列:1054未知列 'projects.currency_name'在“字段列表”中(SQL:选择name网站,<代码>代码,<代码>项目。crurner_nametime_zone from 项目左JOIN 国家/代码>在项目国家/代码>。id_country左JOIN crentrencies on 项目chormer_id = crentrenciesid_currency剩下的加入 timezones on 项目timezone_id = timezonesid_timezone

I have a projects table with the value of the following column:

id_project   name           website         country_id  currency_id  timezone_id
==========.  ====           =======         ==========. ===========. ===========
1            project name.  www.yahoo.com.  10          8            10

and a countries table with the value of the following column:

id_country.      country_name
==========.      ============
10               America

and a currencies table with the value of the following column:

id_currency      currency_name
===========      ============
8                BD

and a timezones table with the value of the following column:

id_timezone      time_onze    country_name
===========      =========    ============
10               Asia/Dhaka   Bangladesh

Now, I want to run a query that should give me name, website, country_name, time_zone, currency_name. BUT you can see the country_name is exist on both table which is countries and timezones. So, the query should give me the country_name from countries table NOT time_zones table.

So, for that I am running following query:

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->get(['name', 'website', 'code', 'projects.currency_name', 'time_zone']);

return response()->json($projects, 200);

But Can't get the data

Error message:

"SQLSTATE[42S22]: Column not found: 1054 Unknown column
'projects.currency_name' in 'field list' (SQL: select name,
website, code, projects.currency_name, time_zone from
projects left join countries on projects.country_id =
countries.id_country left join currencies on
projects.currency_id = currencies.id_currency left join
timezones on projects.timezone_id = timezones.id_timezone)"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍白女子 2025-02-16 10:55:28

实际上,您在做错查询,

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->get(['projects.name', 'projects.website, 'currencies.currency_name', 'timezones.time_zone']);

return response()->json($projects, 200);

我看不到任何代码列您可以确保其来自何处。

Actually you are doing wrong query

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->get(['projects.name', 'projects.website, 'currencies.currency_name', 'timezones.time_zone']);

return response()->json($projects, 200);

I don't see any code column can you make sure where it's comes from.

oО清风挽发oО 2025-02-16 10:55:28

尝试此

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->get(['projects.name', 'projects.website', 'currencies.currency_name', 'timezones.time_zone']);

return response()->json($projects, 200);

或使用Select Helper,

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->select(
['projects.name', 'projects.website', 'currencies.currency_name', 'timezones.time_zone']
)
->get();

return response()->json($projects, 200);

您的数据库中没有代码列。所以我不包括它。对不起,我的英语不好,

希望这对您有帮助

Try this

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->get(['projects.name', 'projects.website', 'currencies.currency_name', 'timezones.time_zone']);

return response()->json($projects, 200);

or with select helper

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->select(
['projects.name', 'projects.website', 'currencies.currency_name', 'timezones.time_zone']
)
->get();

return response()->json($projects, 200);

There is no code column in your DB. So i dont include it. Sorry for my bad english

Hope this help you

末が日狂欢 2025-02-16 10:55:28

列选择中存在错误,因为所有列名都是唯一的,您可以完全忽略列前添加表名。

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->get(['name', 'website', 'currency_name', 'time_zone']);

return response()->json($projects, 200);

There's an error in your column selection, as all the column names are unique you can totally ignore adding the table name in front of columns.

$projects = DB::table('projects')
->leftJoin('countries', 'projects.country_id', '=', 'countries.id_country')
->leftJoin('currencies', 'projects.currency_id', '=', 'currencies.id_currency')
->leftJoin('timezones', 'projects.timezone_id', '=', 'timezones.id_timezone')
->get(['name', 'website', 'currency_name', 'time_zone']);

return response()->json($projects, 200);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文