where-in

MySQL SELECT WHERE IN LIST and NOT IN LIST in the same SQL

前提是你 提交于 2019-12-01 14:17:45
I have this: $ids = "1,2,3,4,5"; $sqlQuery = "SELECT id, moderation_date FROM table_live WHERE id IN (".$ids.")"; $q = $this->CI->db->query($sqlQuery); if($q->num_rows() > 0) { foreach ($q->result() as $row) { $arr[] = $row; } } return $arr; This is just working fine if all ids exist in table_live and return array([id] => 1 [moderation_date] => 2012-04-11 12:55:57).... The problem: If I send a list of ids 1-2-3-4-5 where only 1-2-5 match the IN LIST clause I need to return all in list and for those don't match the list a null value. array([id] => 3 [moderation_date] => null) generate an outer

MySQL SELECT WHERE IN LIST and NOT IN LIST in the same SQL

六月ゝ 毕业季﹏ 提交于 2019-12-01 13:28:52
问题 I have this: $ids = "1,2,3,4,5"; $sqlQuery = "SELECT id, moderation_date FROM table_live WHERE id IN (".$ids.")"; $q = $this->CI->db->query($sqlQuery); if($q->num_rows() > 0) { foreach ($q->result() as $row) { $arr[] = $row; } } return $arr; This is just working fine if all ids exist in table_live and return array([id] => 1 [moderation_date] => 2012-04-11 12:55:57).... The problem: If I send a list of ids 1-2-3-4-5 where only 1-2-5 match the IN LIST clause I need to return all in list and for

Avoid SQL WHERE NOT IN Clause

陌路散爱 提交于 2019-12-01 11:09:49
I have 3 tables listed below: Blog BlogArticle Article ---- ----------- ------- id id------blog_id -id title article_id__/ title This SQL describe what I want: SELECT * FROM `article` WHERE `article`.`id` NOT IN ( SELECT CONCAT(`blog_article`.`article_id`) FROM `blog_article` WHERE `blog_article`.`blog_id` = 1 -- example value ) The problem is, I have a big NOT IN values in that case, and as far as i know it will impact to server performance (I'm not sure since I've never try to benchmark or Google it). Any suggestion? Try this : SELECT * FROM `article` LEFT JOIN `blog_article` ON CONCAT(`blog

Passing an array to sqlite WHERE IN clause via FMDB?

落爺英雄遲暮 提交于 2019-11-30 11:34:57
Is it possible to pass an array to a SELECT … WHERE … IN statement via FMDB? I tried to implode the array like this: NSArray *mergeIds; // An array with NSNumber Objects NSString *mergeIdString = [mergeIds componentsJoinedByString:@","]; NSString *query = @"SELECT * FROM items WHERE last_merge_id IN (?)"; FMResultSet *result = [database executeQuery:query, mergeIdString]; This only works if there is exactly 1 object in the array, which leads me to believe that FMDB adds quotes around the whole imploded string. So I tried passing the array as is to FMDB's method: NSArray *mergeIds; // An array

MySQL select join where AND where

我怕爱的太早我们不能终老 提交于 2019-11-30 06:57:01
问题 I have two tables in my database: Products id (int, primary key) name (varchar) ProductTags product_id (int) tag_id (int) I would like to select products having all given tags. I tried: SELECT * FROM Products JOIN ProductTags ON Products.id = ProductTags.product_id WHERE ProductTags.tag_id IN (1, 2, 3) GROUP BY Products.id But it gives me products having any of given tags, instead of having all given tags. Writing WHERE tag_id = 1 AND tag_id = 2 is pointless, because no rows will be returned.

NHibernate using QueryOver with WHERE IN

百般思念 提交于 2019-11-29 21:15:50
I would create a QueryOver like this SELECT * FROM Table WHERE Field IN (1,2,3,4,5) I've tried with Contains method but I've encountered the Exception "System.Exception: Unrecognised method call: System.String:Boolean Contains(System.String)" Here my code var qOver = _HibSession.QueryOver<MyModel>(() => baseModel) .JoinAlias(() => baseModel.Submodels, () => subModels) .Where(() => subModels.ID.Contains(IDsSubModels)) .List<MyModel>(); Faber I've found the solution!! :-) var qOver = _HibSession.QueryOver<MyModel>(() => baseModel) .JoinAlias(() => baseModel.Submodels, () => subModels)

Passing an array to sqlite WHERE IN clause via FMDB?

帅比萌擦擦* 提交于 2019-11-29 17:15:24
问题 Is it possible to pass an array to a SELECT … WHERE … IN statement via FMDB? I tried to implode the array like this: NSArray *mergeIds; // An array with NSNumber Objects NSString *mergeIdString = [mergeIds componentsJoinedByString:@","]; NSString *query = @"SELECT * FROM items WHERE last_merge_id IN (?)"; FMResultSet *result = [database executeQuery:query, mergeIdString]; This only works if there is exactly 1 object in the array, which leads me to believe that FMDB adds quotes around the

MySQL multiple columns in IN clause

我的梦境 提交于 2019-11-29 09:31:17
I have a database with four columns corresponding to the geographical coordinates x,y for the start and end position. The columns are: x0 y0 x1 y1 I have an index for these four columns with the sequence x0, y0, x1, y1. I have a list of about a hundred combination of geographical pairs. How would I go about querying this data efficiently? I would like to do something like this as suggested on this SO answer but it only works for Oracle database, not MySQL: SELECT * FROM my_table WHERE (x0, y0, x1, y1) IN ((4, 3, 5, 6), ... ,(9, 3, 2, 1)); I was thinking it might be possible to do something

MySQL specify arbitrary order by id

跟風遠走 提交于 2019-11-29 06:36:16
Is it possible to specify an arbitrary order for a MySQL SELECT statement? E.g., SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY (1, 3, 2, 9, 7); The order of the numbers listed directly after IN do not seem to matter. The Scrum Meister FIND_IN_SET function will do the trick SELECT * FROM table_name WHERE id IN (1, 3, 2, 9, 7) ORDER BY FIND_IN_SET(id, '1,3,2,9,7'); http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_find-in-set EDIT: Note the lack of spaces in the string argument of the find_in_set function. Check out mysql's ORDER BY FIELD . I think it will do

SQL use CASE statement in WHERE IN clause

拜拜、爱过 提交于 2019-11-29 01:22:13
Is it posible to use case in where in clause? Something like this: DECLARE @Status VARCHAR(50); SET @Status='published'; SELECT * FROM Product P WHERE P.Status IN (CASE WHEN @Status='published' THEN (1,3) WHEN @Status='standby' THEN (2,5,9,6) WHEN @Status='deleted' THEN (4,5,8,10) ELSE (1,3) END) This code gives the error : Incorrect syntax near ','. No you can't use case and in like this. But you can do SELECT * FROM Product P WHERE @Status='published' and P.Status IN (1,3) or @Status='standby' and P.Status IN (2,5,9,6) or @Status='deleted' and P.Status IN (4,5,8,10) or P.Status IN (1,3) BTW