【发布时间】:2011-07-29 09:29:52
【问题描述】:
我有这两个查询,我想一起加入, 但不够熟练。
谁能帮帮我?
这是选择与搜索匹配的行 其中包含一个 cids 列,而我需要在第二个查询中使用它
SELECT cids,tp FROM searches_done where (keyword='keyword' and city='city' and page='page' and ipp='ipp') LIMIT 1
此查询选择实际结果行,将每个公司的 id 与我们在第一个查询中获得的列分隔值 (cids) 进行比较
此外,它需要将第一个查询中的列 tp 附加到每个结果
SELECT c.*,s.tp FROM companies c WHERE c.id in (cids)
目前,由于我运行了两个查询(通过代码),所以它可以工作, 并在第二个查询中使用我从第一个查询中获得的结果。
searches_done table structure:
id - autoincrement unique int
keyword - varchar(255) - keyword that was searched
page - int - paging page ( i.e: page=0 its the first page etc.. )
city - varchar(255) - keyword for city that was searched
cids - text - comma seperated values of the companies ids
( that were found matching this search )
tp - int - total pages found
ipp - int - itemsperpage ( a search could be done with only 10 items per page
, so diffrent results might be found with 10/100 )
我现在正在创建一个包含sd_id, c_id 的新表以规范化数据库:\
【问题讨论】:
-
那么我应该如何保存它们呢?为这些值创建另一个表似乎很奇怪。还是我错了?
-
我们需要更多关于表结构的信息,但是这通常是解决方案。
-
没有了。。我的表中的所有字段都如图所示。 Companies 表包含更多字段(公司数据,如电话等),但与其他表没有任何连接(除了 ID 之外,如上所示,它也写在 search_done.cids 中)
-
好的,您可以编辑问题来描述您的 searchs_done 表吗?
-
好的,现在我有一个名为 cids 的表,其中包含 sd_id、c_id。好点了吗?