简体   繁体   English

MySQL子查询还是多个查询?

[英]Mysql subqueries or multiple queries?

I'm trying to figure out the best way to get data from a MySQL database and process it. 我正在尝试找出从MySQL数据库获取数据并对其进行处理的最佳方法。 I have 2 tables 'objects', and 'objects_metadata'. 我有2个表“对象”和“ objects_metadata”。 rows in the objects_metadata table belong to rows in the objects table and the link is defined by a 'parent_id' column in objects_metadata that corresponds to an 'id' column in objects. objects_metadata表中的行属于objects表中的行,并且链接是由objects_metadata中的“ parent_id”列定义的,该列对应于object中的“ id”列。 (SQLFiddle below). (下面的SQLFiddle)。

The Scenario 场景

When I search against these tables I'm always looking for rows from the objects table. 当我搜索这些表时,我总是从对象表中查找行。 I sometimes have to query the objects_metadata table to get the right results. 有时我必须查询objects_metadata表以获得正确的结果。 I do this by defining boundaries such as "hasMetadataWithValue". 我通过定义诸如“ hasMetadataWithValue”的边界来实现。 This boundary would run the following query by itself: 此边界将自行运行以下查询:

SELECT * FROM objects 
INNER JOIN objects_metadata ON objects.id=objects_metadata.parent_id 
WHERE objects_metadata.type_id = ? AND objects_metadata.value = ?

Another example boundary "notSelf" would use a query such as: 另一个示例边界“ notSelf”将使用查询,例如:

SELECT * FROM objects WHERE objects.id != ?

My scenario caters for multiple boundaries at a time. 我的情况一次满足多个边界。 For a row from the objects table to be selected it MUST pass all boundaries. 对于要从​​对象表中选择的行, 必须通过所有边界。 (ie if each boundary query was run independently the row would appear in every set of results) (即,如果每个边界查询都独立运行,则该行将出现在每组结果中)

I'm wondering if anyone has any thoughts on the best way to do this? 我想知道是否有人对最好的方法有任何想法?

  • Use each boundary's query as a subquery in a single query on the database (my original goal) 将每个边界的查询用作数据库中单个查询中的子查询(我的最初目标)
  • Run each boundary's query as a full query and then use PHP to process the results 将每个边界的查询作为完整查询运行,然后使用PHP处理结果

I would prefer to make the database do most of the work and spit out the results simply to avoid running a bunch of queries instead of a single one. 我宁愿让数据库完成大部分工作并吐出结果,只是为了避免运行一堆查询而不是一个查询。 Here's the tricky part, I've tried to create a full query using subqueries, but I'm not getting the hang of it at all. 这是棘手的部分,我试图使用子查询来创建完整的查询,但我完全没有把握。 My latest attempt is below: 我的最新尝试如下:

SELECT * FROM objects 
WHERE type_id = 7 
AND confirmed = 1 
AND (SELECT * FROM objects WHERE objects.id != 1) 
AND (SELECT * FROM objects LEFT JOIN objects_metadata ON objects.id=objects_metadata.parent_id WHERE objects_metadata.type_id = 8 AND objects_metadata.value ='male') 
LIMIT 0,20

I can see that the way I'm trying to use these subqueries is obviously wrong, but I can't figure out what the right way is. 我可以看到我尝试使用这些子查询的方式显然是错误的,但是我无法弄清楚什么是正确的方式。

SQL Fiddle is here SQL小提琴在这里

Any insights into the best way of doing this would be much appreciated. 最好的方式做到这一点的任何见解将不胜感激。

I think you can just put those 'boundaries' inside your joined query. 我认为您可以将那些“边界”放入联接的查询中。

SELECT 
    * 
FROM objects LEFT JOIN objects_metadata 
    ON objects.id = objects_metadata.parent_id
WHERE 
    objects_metadata.type_id = 8
    AND objects.confirmed=1 
    AND ( objects.id!=1 )
    AND ( objects_metadata.type_id=8 AND objects_metadata.value='male' ) 
LIMIT 0,20

SQL Fiddle: http://sqlfiddle.com/#!2/0ee42/34 SQL小提琴: http ://sqlfiddle.com/#!2 / 0ee42 / 34

Just mind the same column names for both tables, so you have to specify the exact table as well (eg, objects_metadata.type_id = 8). 只需记住两个表具有相同的列名,因此您还必须指定确切的表(例如,objects_metadata.type_id = 8)。 If I completely misunderstand your question let me know! 如果我完全误解了您的问题,请告诉我! :) :)

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

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