理解 MySQL 中的 JOIN 与 UNION



019.8.3

起步

最近公司接到一个项目,任务是根据需求制表。完整过程是:用 SQL 汇总数据,再写进 Execel 文件中。SQL 这门课倒是大学里学过,过久不用,不记得许多,顶多 SELECT 几下。总之是勉强应付。

这周五接到一个需求,差点应付不过去,卡在联表操作上了。午觉再不敢睡,总结 MySQL 查询时的多表联合。临阵磨枪管用,当测试发来 “OK” 的时候我这样想。

笔记至此,遗忘再寻。



对角线问题


问题

这里有 n * n 矩阵,要求左上到右下的对角线上都为 x,其他地方为 y。x、y 任意值,但不相等。

x y y ... y
y x y ... y
y y x ... y
. . . ... .
. . . ... .
y y y ... x


二分查找


二分查找是一个比较简单的算法,用 C++ 语言实现如下:

template <typename T>
int binary_search(
        const T& key,  // 搜索元素 key
        vector<int>::const_iterator data,  // 数组起始位置
        int N)  // 元素个数
{
    // 左闭右闭
    int low = 0;
    int high = N - 1;

    while (low <= high) {

        int mid = low + (high - low) / 2;

        if (key < *(data + mid))
            high = mid - 1;
        else if (key > *(data + mid))
            low = mid + 1;
        else
            return mid;
    }
    return -1;
}

用法:
// vector<int> arr = {1, 2, 3, 4, 5, 65};
// cout << binary_search(5, arr.cbegin(), 6) << endl;
// cout << binary_search(1, arr.cbegin(), 6) << endl;
// cout << binary_search(4, arr.cbegin(), 6) << endl;


TypeError: super(type, obj): obj must be an instance or subtype of type


问题

今天学习《Python Web 开发实战》自定义转换器这一小节,书中有段代码如下:

class ListConverter(BaseConverter):

    def __init__(self, url_map, separator="+"):
        super(ListConverter, self).__init__(url_map)
        self.separator = urllib.parse.unquote(separator)

    def to_python(self, value):
        return value.split(self.separator)

    def to_url(self, values):
        return self.separator.join(super(ListConverter, self).to_url(value)
                                   for value in values)

倒没什么问题,是可以正常运行的。