The JOIN syntax is used to join multiple tables and supports the following usage methods:
Join multiple query results in the same log topic.
Syntax Format
* | SELECT [column name(KEY)] FROM table1 JOIN_TYPE table2 ON table1.key=table2.key
Where:
When the column name (KEY) is the same in multiple tables, the table name or alias should be specified.
JOIN_TYPE supports the following types (the content in [ ] can be omitted):
[ INNER ] JOIN: Return results that exist in both tables.
LEFT [ OUTER ] JOIN: Return all SELECT results from the left table (table1) even if there is no matching data in the right table (table2).
RIGHT [ OUTER ] JOIN: Return all SELECT results from the right table (table2) even if there is no matching data in the left table (table1).
FULL [ OUTER ] JOIN: Return all SELECT results that meet the conditions as long as matching data exists in one table.
CROSS JOIN: JOIN_CONDITION does not need to be specified (namely, ON table1.key=table2.key), and the Cartesian product of two tables (all possible combinations) is returned.
JOIN_CONDITION, namely, ON table1.key=table2.key. You can also specify filtering conditions at the same time, such as ON (table1.key=table2.key and table2.key='xxx'), which will filter the data in the original tables according to the specified conditions before the JOIN query.
Syntax Example
LEFT JOIN: Query ip, url, and user_id from the log data (log), and query name and gender from the userinfo table based on user_id.
* |
select
log.ip,
log.url,
log.user_id,
userinfo.Name,
userinfo.Gender
from
log
left join userinfo on log.user_id = userinfo.id
limit
10000
Return result:
INNER JOIN: Count the number of error logs (errorCount) and the total number of log entries (allCount) for each URL in the current log topic, and then join them together. The instance also demonstrates the method of using nested subqueries together with JOIN syntax.
* |
select
errorCount,
allCount,
table1.url as url
from
(
select
count(*) as errorCount,
url
where
status_code != 200
group by
url
) as table1
inner join (
select
count(*) as allCount,
url
group by
url
) as table2 on table1.url = table2.url
Return result: