A2-02-16.DML-A Practical Use of MySQL CROSS JOIN Clause

news/2024/11/9 13:13:03

转载自:http://www.mysqltutorial.org/mysql-cross-join/

 

A Practical Use of MySQL CROSS JOIN Clause

 

Summary: in this tutorial, you will learn about the MySQL CROSS JOIN clause and how to apply it to answer some interesting data questions.

Introduction to MySQL CROSS JOIN clause

The CROSS JOIN clause returns the Cartesian product of rows from the joined tables.

Suppose you join two tables using CROSS JOIN. The result set will include all rows from both tables, where each row in the result set is the combination of the row in the first table with the row in the second table. This situation happens when you have no relationship between the joined tables.

The danger thing is that if each table has 1,000 rows, you will get 1,000 x 1,000 = 1,000,000 rows in the result set, which is huge.

The following illustrates the syntax of the CROSS JOIN clause that joins two tables T1 and T2:

Note that different from the INNER JOIN or LEFT JOIN clause, the CROSS JOIN clause does not have the join conditions.

If you add a WHERE clause, in case T1 and T2 has a relationship, the CROSS JOIN works like the INNER JOIN clause as shown in the following query:

 

MySQL CROSS JOIN clause example

We will use the following tables to demonstrate how the CROSS JOIN works.

There are three tables involved:

  1. The products table contains the products master data that includes product id, product name, and sales price.
  2. The stores table contains the stores where the products are sold.
  3. The sales table contains the products that sold in a particular store by quantity and date.

Suppose we have three products iPhoneiPad and Macbook Pro which are sold in two stores Northand South.

To get the total sales for each store and for each product, you calculate the sales and group them by store and product as follows:

MySQL CROSS JOIN GROUP BY example

Now, what if you want to know also which store had no sales of a specific product. The query above could not answer this question.

To solve the problem, you need to use the CROSS JOIN clause.

First, you use the CROSS JOIN clause to get the combination of all stores and products:

MySQL CROSS JOIN stores and products

Next, you join the result of the query above with the query that returns the total of sales by store and by product. The following query illustrates the idea(自己没有测试成功,可能是SQL模式的原因):

MySQL CROSS JOIN query example

Note that the query used the IFNULL function to return 0 if the revenue is NULL (in case the store had no sales).

By using the CROSS JOIN clause this way, you can answer a wide range of questions e.g., find the sales revenue by salesman by month even if a salesman had no sales in a particular month.

转载于:https://www.cnblogs.com/zhuntidaoren/p/9517603.html


http://www.niftyadmin.cn/n/4557339.html

相关文章

【转】Mcrypt响应慢的一个原因

上午的时候, 有同事来找我说上周新上线的一个使用mcrypt的脚本, 响应非常慢, 但是服务器的各项指标都正常, 不知道是什么原因. 经过了解, 一个简单的可重现的脚本如下: <?php $dmcryptText "dummy"; $key "foobar"; $size mcrypt_get_iv_size(MCRYP…

你知道批处理中for语句中%%~xi是什么意思么

我也不太清楚 几乎 可能 也许 好像 ||| 大概 %~$PATH:I 搜索 PATH 环境变量所列出的目录 也就是取%%i的扩展名部分其它的展开操作还有%~I 展开 %I %%~xi将%%i解开到扩展名 如%~dpI 只将 %I 展开到驱动器号和路径 %~zI 将 %I 展开到文件的大小 %~tI 将 %I 展开到文件的日期和时间…

为什么eclipse软件用不了

JavaHome 如:"C:/Program Files/Java" 双击path 就可以编辑变量了 你java程序的路径由于要和其他变量分开 你没有设置变量我的电脑→属性→高级&#xff08;环境变量&#xff09;系统变量里面有一个path设置 在设置变量值里面输入 在前面要加入" ; "就这样…

优先级队列例题

package priorityQueue; import java.util.; import java.time.; /* This program demonstrates the use of a priority queue. 数集&#xff0c;优先级队列会对队列里面的数组进行排序&#xff0c;从小到大。 */ public class PriorityQueueTest { public static void main(S…

关于跳出for的批处理

这个是因为你加了if " ||| break是跳出循环 continue就结束本次循环 在执行下一次循环 1 只能用call了 所以不能像c语言那样嵌套循环语句 因为你要换行 就和c语言一样的 就会退出程序 他会跳到:end这个语句后面正好没有语句了 当程序跳到空格的状态时 而我们的是退出循环执…

变量的就近原则

就近原则(Principle of Proximity)&#xff0d;把相关的操作放在一起&#xff0c;例如让注释靠近它所描述的代码&#xff0c;让控制循环的代码靠近循环本身等。变量的就近原则指尽可能在靠近第一次使用变量的位置声明和定义该变量。就近原则实际上也是变量的作用域最小化的一种…

映射操作,删除,修改,迭代处理条目

要创建一个Employee类&#xff0c;用于存储实例化的键值。 package com.yt.map; import java.util.*;/* This program demonstrates the use of a map with key type String and value type Employee. 这是一个映射操作。 1.先将键/值添加到映射中 2.删除一个映射的值 3.修改一…

帮忙看看我C语言求阶乘的程序错在哪

建议你去看谭浩强的《C语言程序设计 》 %ld" &n);while(i<n)/*分号去掉{ss*i;i;}printf("%d 24 n &s);} 答案补充 printf("%d 那就太大了源程序如下&#xff1a;#include <stdio.h>void main(){int n &n);while(i<n){ss*i;i;}printf(&q…