Alternative to in operator in mysql

拜拜、爱过 提交于 2019-12-11 08:54:52

问题


I see In operator alternative in mysql

I have nearly 25,000 ids.I am using in operator on that.Then i am getting Stackoverflow Exception.Is there any other alternative for IN operator in mysql.

Thanks in advance..


回答1:


If the ID's are in another table:

SELECT * FROM table1 WHERE id IN (SELECT id FROM table2);

then you can use a join instead:

SELECT table1.* FROM table1 INNER JOIN table2 ON table1.id = table2.id;



回答2:


You could do the following:

1 - Create a MySQL Temporary Table

CREATE TEMPORARY TABLE tempIdTable (id int unsigned not null primary key);

2 - Insert All Your ids into the Temporary Table

For every id in your list:

insert ignore into myId (id) values (anId);

(this will have the added bonus of de-duplicating your list of ids ready for the final step)

3 - Join Against the Temporary Table

SELECT t1.* FROM myTable1 t1 INNER JOIN tempIdTable tt ON t1.id = tt.id;

The temporary table will disappear as soon as your connection is dropped so your don't have to worry about dropping it before you create it next time.



来源:https://stackoverflow.com/questions/16562901/alternative-to-in-operator-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!