简体   繁体   English

我想使用 MySQL 中第二个查询中第一个查询的列将两个查询合二为一

[英]I want to use two queries into one using a column from first query in the second one in MySQL

I have these two queries:我有这两个查询:

The first one:第一个:

SELECT * FROM item, user WHERE item_def = '289' 
    AND item_quality = '6' AND user.steam_64 = item.steam_64

The second one:第二个:

select count(item_id) from item where steam_64 = '".$steam_id64."'"

So basically, my first query is returning the users for the matching item (in this case item = 289).所以基本上,我的第一个查询是返回匹配项的用户(在本例中为 item = 289)。 My second query is just looking how many items in total has every user returned in the first query.我的第二个查询只是查看每个用户在第一个查询中返回的项目总数。

I would like to have that count in the first query.我想在第一个查询中有这个计数。

You can just copy the second query and paste it into the select list of the first.您可以复制第二个查询并将其粘贴到第一个查询的 select 列表中。

SELECT
  item.*,
  user.*,
  (select count(item_id) from item where steam_64 = '$steamid64') AS `count`
FROM
  item
INNER JOIN
  user
ON
  item.steam_64 = user.steam_64
WHERE
  item.item_quality = 6
AND
  item.item_def = 289

I feel like a mother saying to her child eat your vegetables , but... write out your joins and don't quote your numbers .我感觉就像一位母亲对她的孩子说吃你的蔬菜,但是……写下你的加入,不要引用你的数字

I believe this is simply a matter of adding a GROUP BY clause since all you're interested from the second table is just the count.我相信这只是添加一个 GROUP BY 子句的问题,因为您对第二个表感兴趣的只是计数。

Putting an additional SELECT statement in the main SELECT statement will cause an additional SELECT query to be done for every row returned -- which could lengthen query time dramatically.在主 SELECT 语句中添加额外的 SELECT 语句将导致对返回的每一行执行额外的 SELECT 查询——这可能会显着延长查询时间。

Here's how I'd to this:这是我的做法:

SELECT
  count (*) as count_of_items,
  user.*
FROM
  user join item on item.steam_64 = user.steam_64 
WHERE item.item_quality = 6 AND item.item_def = 289
GROUP BY user.steam_64

The GROUP BY clause just has to be done based on some column that's the same for every joined row. GROUP BY 子句只需要基于对于每个连接行都相同的列来完成。

暂无
暂无

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

相关问题 两个MySQL查询合二为一,第二个查询基于第一个查询 - Two MySQL queries in one, with the second query based on first query 将两个查询合并为一个需要第一个查询中的值的查询 - Combine the two queries into one that needs a value from second query in first 我想将第一个SQL表中的数据插入第二个SQL表中,而第二个表中不存在额外的列 - I want to insert data from first SQL table into second one also with an extra column not present in the second one mysql:搜索两列:第一列,然后第二列 - mysql : search two columns : one column first then second column 如何使用第二个第一个查询的结果启动两个sql查询? - How to launch two sql queries using result of first one in second? 我如何在MySQL中查询两个日期,其中datefrom在同一表的第二列中? - How do I query two dates in mysql where the datefrom is in one column and dateto is in the second column of the same table? 使用来自一个mysql的结果进行第二次查询 - using results from one mysql for a second query SQL:如何将两个复杂查询合并为一个,其中第二个需要来自第一个的数据 - SQL: How to merge two complex queries into one, where the second one needs data from the first one 将两个MYSQL查询列结果合并为一个,以在ORDER BY中使用 - Combine two MYSQL Queries column result into one to use in ORDER BY 两个mysql查询和2个$ results。 我可以仅使用一个mysql查询获得相同的2 $ results吗? - Two mysql queries and 2 $results. Can I get the same 2 $results using only one mysql query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM