如何在laravel中联接三个表

时间:2019-11-14 07:02:37

标签: laravel

我试图在laravel中联接三个表并显示联接的项目。但是它不能正常工作。缺少一些值。当我在类别(例如:categoryid)中显示字段时,它不显示整个行值和某些列字段都丢失。请帮助我进行更正。

$products = DB::table('categories')
        ->leftjoin('subcategories', 'categories.category_id', '=', 'subcategories.category_id')
        ->leftjoin('products', 'products.subcat_id', '=', 'subcategories.subcat_id')
        ->get()->toArray();

2 个答案:

答案 0 :(得分:0)

使用口才也许更清洁。下面是示例

explode

如果使用口才

val df = spark.read.json(Seq(json).toDS.rdd)
df.show(10, false)
df.printSchema

df: org.apache.spark.sql.DataFrame = [Item Creation Time: string, Item Version: double ... 3 more fields]
+-------------------+------------+--------------------------------+----------------------------------------+---------------------------------------------------+
|Item Creation Time |Item Version|Payments                        |Prod                                    |Trade Dictionary                                   |
+-------------------+------------+--------------------------------+----------------------------------------+---------------------------------------------------+
|2019-04-14 14:15:09|1.0         |[WrappedArray([2019-04-11,Rej])]|[WrappedArray([2019-04-16,true]),Driver]|[India,1,Global,XXXXXXXXXXXXXXX,[false,true,false]]|
+-------------------+------------+--------------------------------+----------------------------------------+---------------------------------------------------+
root
 |-- Item Creation Time: string (nullable = true)
 |-- Item Version: double (nullable = true)
 |-- Payments: struct (nullable = true)
 |    |-- Payment Details: array (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- Payment Date: string (nullable = true)
 |    |    |    |-- Payment Type: string (nullable = true)
 |-- Prod: struct (nullable = true)
 |    |-- FX Legs: array (nullable = true)
 |    |    |-- element: struct (containsNull = true)
 |    |    |    |-- Spot Date: string (nullable = true)
 |    |    |    |-- Value: boolean (nullable = true)
 |    |-- Type: string (nullable = true)
 |-- Trade Dictionary: struct (nullable = true)
 |    |-- Country: string (nullable = true)
 |    |-- TradeNumber: string (nullable = true)
 |    |-- TradeRegion: string (nullable = true)
 |    |-- Value: string (nullable = true)
 |    |-- action: struct (nullable = true)
 |    |    |-- Action1: boolean (nullable = true)
 |    |    |-- Action2: boolean (nullable = true)
 |    |    |-- Action3: boolean (nullable = true)


val flat = df
    .select($"Item Creation Time", $"Item Version", explode($"Payments.Payment Details") as "row")
    .select($"Item Creation Time", $"Item Version", $"row.*")
flat.show

flat: org.apache.spark.sql.DataFrame = [Item Creation Time: string, Item Version: double ... 2 more fields]
+-------------------+------------+------------+------------+
| Item Creation Time|Item Version|Payment Date|Payment Type|
+-------------------+------------+------------+------------+
|2019-04-14 14:15:09|         1.0|  2019-04-11|         Rej|
+-------------------+------------+------------+------------+

答案 1 :(得分:0)

尝试

$data = DB::table('categories')
    ->join('subcategories', 'categories.category_id', '=', 'subcategories.category_id')
    ->join('products', 'products.subcat_id', '=', 'subcategories.subcat_id')
    ->get()
    ->toArray();

尝试使用内部联接,并且在获取数据时,您具有相同的列名,我相信这就是为什么它无法识别它试图指定列值的原因。 像:select('products.name','subcategories.name')