SQL查询:将4个表连接到一个

时间:2018-04-22 12:31:13

标签: mysql sql

我尝试了所有类似的问题,但我无法得到我想要的东西。

需要帮助加入表格。

1. Table bookings: 
id = 1 customer_id = 1 booking_details = 'venue etc'

2. Table customers: 
id = 1 name = 'John Citizen'

3. Table: booking_objects: 
id = 1 booking_id = 1 object_id = 1
id = 2 booking_id = 1 object_id = 2

4. Table objects: 
id = 1 name = 'Object 1' 
id = 2 name = 'Object 2'

我想加入表格,我得到“Venue等,Jhon Citizen,Oject 1,Object 2”

目前我正在使用以下查询获取“Venue etc,Jhon Citizen”

$this->db->select("bookings.bookig_details, customers.name as customer_name, objects.name as object_name")->join("customers", "customers.id = bookings.customer_id", "left outer")

2 个答案:

答案 0 :(得分:0)

在你的要求中,提到Venue等,Jhon Citizen,对象1,对象2。对象1和对象2属于同一列,即objects.name。

根据我的理解,您需要booking_details,customer_name,obj_name是您需要的列。

select b.booking_details, c.name as customer_name, o.name as obj_name 
bookings b
inner join
customers c
on b.customer_id  = c.id
inner join
booking_objects bo
on b.id = bo.id
inner join 
objects o
on bo.object_id = o.id

您可以根据需要更改联接类型。

为了清晰起见,最好在问题中使用必需的列,而不是使用所需输出的值。

答案 1 :(得分:0)

您需要使用Collector

像这样。

$> java Test "10D 9c 8p 7H"
[10, 9, 8, 7]

$> java Test "10E 9C 8P 7H"
Exception in thread "main" java.lang.IllegalArgumentException: Input is malformed.
        at Test.main(Test.java:10)

$> java Test "10D 9C 8P 7H 10D 9C 8P 7H"
Exception in thread "main" java.lang.IllegalArgumentException: Input is malformed.
        at Test.main(Test.java:10)

sqlfiddle:http://sqlfiddle.com/#!9/594973/14