php - 如何连接两个具有不同字符串的大表

标签 php mysql sql indexing

我有两个表

table1
customer_id 
101
102
103 

和表2

customer_id country_id 
  AO-101      1
  AO-102      2    
  AO-103      3

这两个表都是非常大的表,我使用 CONCAT(table1.customer_id) 与 table2 连接

上述所有字段都是索引字段

加入他们并获得国家 1 的所有客户需要花费大量时间

谁能帮帮我?

最佳答案

你可以试试这个伙伴:

SELECT * FROM table1  
JOIN table2 ON CONCAT('AO-', table1.customer_id) = table2.customer_id  
WHERE table2.country_id = 1;  

或者这个:

SELECT * FROM table2
JOIN (
    SELECT CONCAT('AO-', customer_id) AS in_customer_id, table1.* FROM table1
) AS table1 ON table1.in_customer_id = table2.customer_id
WHERE table2.country_id = 1;

关于php - 如何连接两个具有不同字符串的大表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28758547/

相关文章:

php - 第二次更新 2 个表未使用 Ajax 更新

mysql - 将导航图点存储在数据库中

sql - 从联接表创建 JSON

sql - 锁定插入件

php - 有没有办法使用 mod_rewrite 从我的网站中删除 .html 和 .php 代码?

php - Laravel 5.4 及以上版本 : Nothing posting to pivot table

php - 在分页中旋转 mysql 查询

mysql - mysql 中的外键约束。我无法修复它吗?

javascript - MySQL 引号、双引号困惑

sql - 如何连接多行?