[英]SELECT in WHERE LIKE clause
so I have 2 tables country_codes and Database. 所以我有2个表格country_codes和Database。 I need to retrive all columns from the database if the country code exist on the table country codes.
如果表中的国家/地区代码中存在国家/地区代码,则需要从数据库中检索所有列。 For example, I have 123 on my country codes table, I need to select all columns if the Phone_Number Column's first 3 digit is 123. So I will select the number 1234567.
例如,我的国家代码表中有123,如果Phone_Number列的前3位数字是123,则需要选择所有列。因此,我将选择数字1234567。
Table Name : Country_Codes 表名称:Country_Codes
+---------------+
|Country_Code |
+---------------+
| 123 |
| 456 |
| 556 |
| 231 |
+---------------+
Table Name : Database 表名:数据库
+---------------+--------+
|Phone_Number | Used |
+---------------+--------+
| 1234567 | No |
| 4568823 | No |
| 5562143 | No |
| 5582143 | No |
| 2319042 | No |
+---------------+--------+
The Result that I want is 我想要的结果是
Table Name : Database 表名:数据库
+---------------+--------+
|Phone_Number | Used |
+---------------+--------+
| 1234567 | No |
| 4568823 | No |
| 5562143 | No |
| 2319042 | No |
+---------------+--------+
I have tried using 我尝试使用
SELECT * FROM database
WHERE database.phone_number LIKE (SELECT country_code FROM country_codes);
But I keep on getting an error. 但是我一直在出错。
Before post the solution, some considerations: 发布解决方案之前,请注意以下事项:
DATABASE
DATABASE
的表 Try this: 尝试这个:
SELECT d.*
FROM database d
JOIN Country_Codes cc
ON cc.Country_Code like concat(d.Phone_Number, '%')
Use exists and left keywords and get the result :
CREATE TABLE #Country_Codes(Country_Code VARCHAR(100))
INSERT INTO #Country_Codes(Country_Code)
SELECT 123 UNION ALL
SELECT 456 UNION ALL
SELECT 556 UNION ALL
SELECT 231
CREATE TABLE #Country_Database(Phone_Number VARCHAR(100), Used VARCHAR(100))
INSERT INTO #Country_Database(Phone_Number , Used)
SELECT '1234567','No' UNION ALL
SELECT '4568823','No' UNION ALL
SELECT '5562143','No' UNION ALL
SELECT '5582143','No' UNION ALL
SELECT '2319042','No'
SELECT Phone_Number,Used
FROM #Country_Database
WHERE EXISTS(SELECT 1 FROM #Country_Codes WHERE Country_Code =
LEFT(Phone_Number,3))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.