php - 加入多个表和搜索参数

标签 php mysql

我有包含 2 个表的数据库,其中 1 个用于存储客户 ID 和客户信息。第二个表使用键/值排序,因为我需要存储未定义的值而不需要更改客户表。

表结构

表客户:

=================
    id | status
=================

表 customers_info:

 =======================================
   id | id_customer | key | value
 =======================================

内容示例:

表客户:

=================
    id | status
    1  | 1
    2  | 1
    3  | 1
==================

表 customers_info

=======================================
    id | id_customer | key | value
    1  | 1           | name| Doe
    2  | 1           | age | 25
    3  | 1           | city| NY
    4  | 2           | name| Smith
    5  | 2           | age | 26
    6  | 3           | age | 30
=======================================

我只是查询我的表

SELECT ci.id_customer AS CustomerId, 
       MAX(CASE WHEN ci.key = 'name' THEN ci.value ELSE '' END) AS CustomerName, 
       MAX(CASE WHEN ci.key = 'age' THEN ci.value ELSE '' END) AS CustomerAge, 
       MAX(CASE WHEN ci.key = 'city' THEN ci.value ELSE '' END) AS CustomerCity
FROM customers_info ci 
GROUP BY ci.id_customer
ORDER BY ci.id_customer;

我可以获取所有客户,但无法使用年龄查询参数,例如,我尝试搜索每个年龄为 25 岁的客户,但使用此查询我无法获取客户的所有值...

SELECT ci.id_customer AS CustomerId, 
       MAX(CASE WHEN ci.key = 'name' THEN ci.value ELSE '' END) AS CustomerName, 
       MAX(CASE WHEN ci.key = 'age' THEN ci.value ELSE '' END) AS CustomerAge, 
       MAX(CASE WHEN ci.key = 'city' THEN ci.value ELSE '' END) AS CustomerCity
FROM customers_info ci 
LEFT JOIN customers_info on customers_info.age = '25'
GROUP BY ci.id_customer
ORDER BY ci.id_customer;

我想得到

=======================================
   id_customer | name | age | city
   1           | Doe  | 25  | NY
=======================================

最佳答案

我不确定你这样做的方式是否是最好的方式(我不确定我会使用最大值来做到这一点,我也不认为我会按照你这样做的方式创建表 - 因为在这个如果您正在执行几个额外的操作以获得您正在使用的键值表) - 但这是我的 -

首先,我的创建语句:

create database CustomerLinkQuestion;

use CustomerLinkQuestion;

create table customers (
id int,
status int
);

create table customer_info (
id int,
id_customer int,
k varchar(255),
v varchar(255)
);

无论出于何种原因(这可能是因为我不太使用 MySQL)键和值不起作用(可能是保留的)所以我使用 k 和 v 作为键和值段。

核心概念是您将不得不使用某种子查询或 View ( View 会更好)-

select * From (
SELECT ci.id_customer AS CustomerId,
       MAX(CASE WHEN ci.k = 'name' THEN ci.v ELSE '' END) AS CustomerName,
       MAX(CASE WHEN ci.k = 'age' THEN ci.v ELSE '' END) AS CustomerAge,
       MAX(CASE WHEN ci.k = 'city' THEN ci.v ELSE '' END) AS CustomerCity
FROM customer_info ci
GROUP BY ci.id_customer
ORDER BY ci.id_customer) as sub
where sub.CustomerAge = 25

内部(括号中称为 sub 的部分)正是您发布的内容(除了 - 无论出于何种原因在我的测试服务器上我使用“客户”而不是“客户”)。然后是子查询,然后是减少子查询的 where 子句。

再次 - 我强烈建议您查看您的架构和设计,因为您似乎正在尝试为根本没有意义的内容创建代码表。您最终会在客户表中得到固定数量的字段,每个字段都与特定客户相关联。当您在这里做您正在做的事情时,您正在执行更多根本不需要的数据库操作。

祝你好运!

阿列克谢

关于php - 加入多个表和搜索参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32910959/

相关文章:

php - fatal error : Cannot pass parameter 8 by reference in PHP

javascript - 如何在单击新页面上的按钮时显示数据库中的信息?

php - 不正确的算术输出

php - 如何根据数据库值动态定义常量?

java - 根据用户名在android上检索json数据

php - 使用 php 解析 pdf

java - 解决 Java 中时间错误的错误格式?

mysql - 按 MySQL 中的混合字母数字列排序

php - 单查询和多查询哪个更好?

java - Struts 1 和连接池