简体   繁体   English

PHP / SQL 从数据库中选择 id 列表并在查询中使用它

[英]PHP / SQL Select list of id from DB and use it in query

Hi i made a page on my website to find quest in my database.嗨,我在我的网站上创建了一个页面,以在我的数据库中查找任务。

The method i used for now, its working but i have to update it everytime.我现在使用的方法,它有效,但我每次都必须更新它。

$demande = "SELECT * FROM quest_quest WHERE id IN (584,585,586,587,589,599,601,617,608,615,616) LIMIT ".(($cPage-1)*$perPage).",$perPage"; 

instead i would like to query my database and use the id in my query相反,我想查询我的数据库并在我的查询中使用 id

I have a table with TagID and IDquest, i want to make a query to hold all questid and use it on my query我有一个带有 TagID 和 IDquest 的表,我想进行查询以保存所有 questid 并在我的查询中使用它

Example:例子:

$questsfind = mysql_query("SELECT * FROM `quest_statetag` WHERE idTag ='8'");

I want to have a variable like that (221, 587, 1155, 1255, 5585)我想要一个这样的变量 (221, 587, 1155, 1255, 5585)

$demande = "SELECT * FROM quest_quest WHERE id IN ('".$questsfind."') LIMIT ".(($cPage-1)*$perPage).",$perPage"; 

nb: Sry for my english its not my mother tongue :S注意:请注意我的英语不是我的母语:S

Not sure if I understand the question, but I believe you are looking to hold a bunch of IDs in a variable (from a query) and then create another query from that?不确定我是否理解这个问题,但我相信您希望在变量中保存一堆 ID(来自查询),然后从中创建另一个查询?

$first = mysql_query("SELECT * FROM quest_quest WHERE id IN (1,2,3,4)");

if( ! $first )
    die('Something went wrong...');

$ids = array();

while( $row = mysql_fetch_assoc($first) ) {
    $ids[] = $row['id'];
}

$query_ids = implode(',', $ids);

$second = mysql_query("SELECT * FROM quest_statetag WHERE idTag IN ($query_ids)");

Hope that helps...希望有帮助...

first you have to convert your associative array to string首先,您必须将关联数组转换为字符串

$questsfinds = implode(',', $questsfind);

Then you can try FIND_IN_SET method然后你可以试试 FIND_IN_SET 方法

SELECT * FROM quest_quest WHERE FIND_IN_SET(id,'$questsfinds');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM