MYSQL:选择列包含更多字符串的行?

时间:2018-02-09 13:34:33

标签: mysql

在我的表格中,我有这种情况:

+-------+---------------+--------------+----------+
|  ID   |     CODE      |    VALUE     |   Etc... |
+-------+---------------+--------------+----------+
|   1   |    10a10a     | a1, b1, a2,  |          |
|       |               | c1, c5, e7   |          |
+-------+---------------+--------------+----------+
|   2   |    15cef1     | b1, n6, p1,  |          |
|       |               | y3, e7, d9   |          |
+-------+---------------+--------------+----------+
|                                                 |
|                    Etc....                      |

当我运行以下查询时:

 SELECT * FROM code WHERE value IN ('10a10a') AND facilities LIKE ('%b1%')

我经常从服务器获得响应。但是,使用facilities LIKE ('%b1%', '%c5%')执行查询服务器不会返回任何结果。

在同一列中搜索多个值的正确方法是什么?

2 个答案:

答案 0 :(得分:0)

您可以使用单独的OR子句查询该内容。

SELECT * FROM code WHERE value IN ('10a10a') AND (facilities LIKE ('%b1%') OR facilities LIKE ('%c5%'))

注意由附加括号包围的OR语句的分组。

但是,如果需要对其进行查询,则在“值”列中存储多个值,可能意味着应将值列标准化并移动到其自己的单独表中,然后可以使用{{1}查询}语句,没有使用JOIN运算符的通配符模式的开销。

答案 1 :(得分:0)

你需要使用或在寻找2个值时使用:

'use strict';
const path = require('path');
const config = require('../config');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const packageConfig = require('../package.json');

exports.assetsPath = function (_path) {
    const assetsSubDirectory = process.env.NODE_ENV === 'production'
        ? config.build.assetsSubDirectory
        : config.dev.assetsSubDirectory;

    return path.posix.join(assetsSubDirectory, _path);
};

exports.cssLoaders = function (options) {
    options = options || {};

    const cssLoader = {
        loader: 'css-loader',
        options: {
            sourceMap: options.sourceMap,
        },
    };

    const postcssLoader = {
        loader: 'postcss-loader',
        options: {
            sourceMap: options.sourceMap,
        },
    };

    // generate loader string to be used with extract text plugin
    function generateLoaders(loader, loaderOptions) {
        const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader];

        if (loader) {
            loaders.push({
                loader: loader + '-loader',
                options: Object.assign({}, loaderOptions, {
                    sourceMap: options.sourceMap,
                }),
            });
        }

        // Extract CSS when that option is specified
        // (which is the case during production build)
        if (options.extract) {
            return ExtractTextPlugin.extract({
                use: loaders,
                fallback: 'vue-style-loader',
            });
        } else {
            return ['vue-style-loader'].concat(loaders);
        }
    }

    // https://vue-loader.vuejs.org/en/configurations/extract-css.html
    return {
        css: generateLoaders(),
        postcss: generateLoaders(),
        less: generateLoaders('less'),
        sass: generateLoaders('sass', {indentedSyntax: true}),
        scss: generateLoaders('sass'),
        stylus: generateLoaders('stylus'),
        styl: generateLoaders('stylus'),
    };
};

// Generate loaders for standalone style files (outside of .vue)
exports.styleLoaders = function (options) {
    const output = [];
    const loaders = exports.cssLoaders(options);

    for (const extension in loaders) {
        const loader = loaders[extension];
        output.push({
            test: new RegExp('\\.' + extension + '$'),
            use: loader,
        });
    }

    return output;
};

exports.createNotifierCallback = () => {
    const notifier = require('node-notifier');

    return (severity, errors) => {
        if (severity !== 'error') return;

        const error = errors[0];
        const filename = error.file && error.file.split('!').pop();

        notifier.notify({
            title: packageConfig.name,
            message: severity + ': ' + error.name,
            subtitle: filename || '',
            icon: path.join(__dirname, 'logo.png'),
        });
    };
};