[英]Mysql Right join with mutiple condtion not working
我有兩個表 table1 和 table2 並且當將 table2 與 table1 正確連接時,我沒有得到該行。 在這里,我還附上了小提琴鏈接
CREATE TABLE `table1` (
`orderid` int(11) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`orderid`)
);
CREATE TABLE `table2` ( `fields_id` INT NOT NULL AUTO_INCREMENT , `table1_order_id` INT(11) NOT NULL , `field_value` VARCHAR(100) NOT NULL , `fname` VARCHAR(100) NOT NULL , PRIMARY KEY (`fields_id`)) ENGINE = InnoDB;
INSERT INTO `table1` (`orderid`) VALUES (1), (2), (3), (4), (5), (6);
INSERT INTO `table2` (`fields_id`, `table1_order_id`, `field_value`, `fname`) VALUES (NULL, '1', 'karthi', 'name');
INSERT INTO `table2` (`fields_id`, `table1_order_id`, `field_value`, `fname`) VALUES (NULL, '2', 'karthi', 'name');
INSERT INTO `table2` (`fields_id`, `table1_order_id`, `field_value`, `fname`) VALUES (NULL, '3', 'selva', 'name');
INSERT INTO `table2` (`fields_id`, `table1_order_id`, `field_value`, `fname`) VALUES (NULL, '1', 'salem', 'city');
INSERT INTO `table2` (`fields_id`, `table1_order_id`, `field_value`, `fname`) VALUES (NULL, '2', 'chennai', 'city');
INSERT INTO `table2` (`fields_id`, `table1_order_id`, `field_value`, `fname`) VALUES (NULL, '3', 'mumbai', 'city');
select table1.*
from table1
right JOIN table2 ON (
table2.table1_order_id = table1.orderid
AND
( table2.field_value LIKE '%karthi%'
AND table2.fname = 'name' )
AND (
table2.field_value LIKE '%salem%'
AND table2.fname = 'city'
)
)
where 1 group by table1.orderid
上面我寫了 select 查詢,但它返回 null 結果,但我期待 output 如下所示,
訂單號 1
您必須將AND
子句更改為OR
子句,您將獲得如下兩條記錄:
select table1.*
from table1
right JOIN table2 ON
table2.table1_order_id = table1.orderid
where
( table2.field_value LIKE '%karthi%'
AND table2.fname = 'name' )
OR (
table2.field_value LIKE '%salem%'
AND table2.fname = 'city'
)
group by table1.orderid
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.