Laravel从多个表获取数据
我有一个项目表,具有以下列的值:
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_name
,time_zone
from项目
左JOIN 国家/代码>在项目
。国家/代码>。
id_country
左JOINcrentrencies
on项目
。chormer_id
=crentrencies
。id_currency
剩下的加入timezones
on项目
。timezone_id
=timezones
。id_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: selectname
,website
,code
,projects
.currency_name
,time_zone
fromprojects
left joincountries
onprojects
.country_id
=countries
.id_country
left joincurrencies
onprojects
.currency_id
=currencies
.id_currency
left jointimezones
onprojects
.timezone_id
=timezones
.id_timezone
)"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实际上,您在做错查询,
我看不到任何
代码
列您可以确保其来自何处。Actually you are doing wrong query
I don't see any
code
column can you make sure where it's comes from.尝试此
或使用Select Helper,
您的数据库中没有
代码
列。所以我不包括它。对不起,我的英语不好,希望这对您有帮助
Try this
or with select helper
There is no
code
column in your DB. So i dont include it. Sorry for my bad englishHope this help you
列选择中存在错误,因为所有列名都是唯一的,您可以完全忽略列前添加表名。
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.