where-in

Laravel 4: add whereIn clause to a join condition

感情迁移 提交于 2019-12-11 09:37:11
问题 I want to run the below query SELECT * FROM order_item JOIN order ON (order_item.order_id = order.id AND order.order_status IN ('payment_completed','refund_requested')) WHERE order_item.item_id=1; I tried like below. OrderItem::join('order', function($join){ $join->on('order.id','=','order_item.order_id'); $join->whereIn('order.order_status',array('payment_completed','refund_requested')); })->where('order_item.item_id','=','1')->get(); I know this is wrong. What is the right way to use

MySQL WHERE IN () + AND , PDO returns only one row

本秂侑毒 提交于 2019-12-11 09:33:03
问题 following query returns all wanted results if entered in phpmyadmin: SELECT postid, voting FROM postvotes WHERE userid = 1 AND postid IN (1007,1011,1012,1013,1014, 1015,1016,1017,1018,1019,1020,1021,1023,1025,1026, 1027,1028,1029,1030,1031) But PDO fails to fetchAll(). It just returns the first match like fetch(). What's wrong? PHP Code: private function userPostVotings( $postIDs ) { // $postIDs contains a string like 1,2,3,4,5,6,7... // generated through implode(',', idArray) try {

laravel whereIn multiple columns

谁说我不能喝 提交于 2019-12-11 05:38:11
问题 I am looking for laravel way for this query: select * from `table` where (product_id, request_id) NOT IN ((66, 10),(76,23)) Perhaps something like: $ids = =array( ['66', '10'], ['76', '23'] ) DB::table('table')->whereNotInMultiple(['product_id', 'request_id'], $ids)->get(); How do I do this in laravel? 回答1: DB::table('table')->whereNotIn('product_id', ['66','10']) ->whereNotIn('request_id', ['76', '23'])->get(); 来源: https://stackoverflow.com/questions/56735665/laravel-wherein-multiple-columns

MySQL WHERE IN Query - ORDER BY Match

人走茶凉 提交于 2019-12-08 19:32:15
问题 I'm trying to rephrase my question, cause my last one wasn't clear to everyone. This is my Test Table +----------+---------+-------+ | rel_id | genre | movie | +----------+---------+-------+ | 1 | 1 | 3 | | 2 | 8 | 3 | | 3 | 3 | 3 | | 4 | 2 | 5 | | 5 | 8 | 5 | | 6 | 3 | 5 | | 7 | 1 | 8 | | 8 | 8 | 8 | | 9 | 3 | 8 | | 10 | 5 | 9 | | 11 | 7 | 9 | | 12 | 9 | 9 | | 13 | 4 | 9 | | 14 | 12 | 9 | | 15 | 1 | 10 | | 16 | 8 | 10 | | 17 | 3 | 10 | | 18 | 5 | 10 | | 19 | 1 | 11 | | 20 | 2 | 11 | | 21 | 8

MYSQL: Limiting rows per whereIn()

我与影子孤独终老i 提交于 2019-12-08 12:48:36
问题 users Table id user_comments Table id | user_id | content | created_at I have a list of user IDs, and I want to grab the latest 3 comments for each user id. SELECT * FROM user_comments WHERE user_id IN (1, 2, 3, 4, 5) ORDER BY created_at DESC LIMIT 3; This will grab the last 3 comments from all matching IDs, I want the last 3 comments for each ID. 1 query without unions preferred. I have tried right joining the table on itself but I cant seem to get it right. ** Edit: I cannot rely on the id

SELECT WHERE IN with GROUP_CONCAT as input

别说谁变了你拦得住时间么 提交于 2019-12-08 10:28:28
问题 Please read this before continuing: Filter an unfiltered table against a whitelist table So, I currently have a whitelist table set up as shown in the referenced link, and I'm encountering yet another issue brought up by said table, that is, to check the UNIQUENESS of each column. As MySQL's specification, it is not possible to set NULL column as UNIQUE, so, I've decided to come up with a different solution to check if rows are duplicated or not by using a SELECT GROUP BY query as follows.

How to make Laravel whereIn not sorted automatically

时光毁灭记忆、已成空白 提交于 2019-12-08 03:06:26
my array from $temp is Array ( [0] => 22 [1] => 26 [2] => 20 [3] => 24 ) or 22|26|20|24 when I use whereIn like this $robjeks = DB::table('objek')->whereIn('id', $temp)->get(); the result is 20|22|24|26| it's automatically sorted. I want it's not sorted. how to make it same like 22|26|20|24 ? thanks for your attention. Lionel Chan This has nothing to do with Laravel. Read here first: avoid Sorting by the MYSQL IN Keyword Then, to do this, you can use this code: $temp = [22, 26, 20, 24]; $tempStr = implode(',', $temp); $robjeks = DB::table('objek') ->whereIn('id', $temp) ->orderByRaw(DB::raw(

Pass string into SQL WHERE IN

匆匆过客 提交于 2019-12-06 02:23:55
问题 I am working on a query page where a user selects a value which represents different types, each identified by an ID. The problem is selecting these IDs from the data base using the WHERE IN method. This is my SQL statement SELECT M.REG_NO, T.TYPE_ID FROM MAIN AS M INNER JOIN CLASSIFICATION AS C ON M.REG_NO = C.REG_NO INNER JOIN TYPE AS T ON T.TYPE_ID = C.TYPE_ID WHERE T.TYPE_ID IN (@Types) it will work for one single value, eg. 46, but NOT if the value is in brackets, eg. (46) or ('46'), the

MYSQL query WHERE IN vs OR

≯℡__Kan透↙ 提交于 2019-12-05 04:08:33
问题 I have developed a system using an OR query: SELECT * FROM tableA JOIN tableB ON (idA = idB) WHERE idA = 1 OR idA = 2 OR idA = 3 OR idA = 4 OR idA = 5 ...... OR idA=100 Compare with query IN: SELECT * FROM tableA JOIN tableB ON (idA = idB) WHERE idA IN (1,2,3,4,5,......,100) What is the best query in a MYSQL database? 回答1: Use IN. IN will use an index. OR will (afaik) not use an index. Also, and this point is not to be sneezed at, the IN version: uses less code is easier to maintain is easier

Why is this mySQL query extremely slow?

£可爱£侵袭症+ 提交于 2019-12-05 02:57:13
问题 Given is a mySQL table named "orders_products" with the following relevant fields: products_id orders_id Both fields are indexed. I am running the following query: SELECT products_id, count( products_id ) AS counter FROM orders_products WHERE orders_id IN ( SELECT DISTINCT orders_id FROM orders_products WHERE products_id = 85094 ) AND products_id != 85094 GROUP BY products_id ORDER BY counter DESC LIMIT 4 This query takes extremely long, around 20 seconds. The database is not very busy