[英]combine mysql tables with query UNION or JOIN?
brand new to complex mysql queries. 全新的复杂mysql查询。 I have a database schema that includes a pair of tables that I need to combine:
我有一个数据库架构,其中包含一对需要组合的表:
In the first table avatars,
I have columns user_id
, url
, and name,
with a single row for each unique user_id.
在第一个表格
avatars,
我有user_id
, url
和name,
列,每个唯一的user_id.
都有一行user_id.
In the second table interpretations
I have user_id
and scan_index
plus 3 other fields. 在第二个表
interpretations
我有user_id
和scan_index
以及其他3个字段。 I'd like to generate a mysql result that matches a specific scan_index
from interpretations,
includes all columns from this table and additional corresponding fields url
, and name
from avatars.
我想生成一个与
interpretations,
中的特定scan_index
匹配的mysql结果interpretations,
包括该表中的所有列以及其他对应字段url
和avatars.
name
avatars.
Should I be using JOIN or UNION to do this query? 我应该使用JOIN还是UNION进行此查询?
JOIN is used to combine both tables on the basis of column values (here in your case, i guess, its user_id), your query can be something like this : JOIN用于在列值的基础上合并两个表(在这里,我想是它的user_id),您的查询可以是这样的:
SELECT url, name, scan_index,...
FROM avatars
LEFT JOIN interpretations
ON avatars.user_id = interpretations.user_id
WHERE interpretations.scan_index = *your_specific_value*
You need to use join
. 您需要使用
join
。 To explain it very simply, a join
combines 2 tables by placing the data from the 2 tables next to each other, while a union
combines 2 queries by placing the data below each other. 非常简单地解释它,一个
join
通过将彼此相邻从2个表中的数据,而联合收割机2台union
通过将低于对方的数据集2次的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.