Answer the question
In order to leave comments, you need to log in
How to find the balance for each user (sum per operator of each user)?
How can I calculate the amount by numbers with a specific operator? For example, I want to define with the operator code "90". But if you use the query below, then problems immediately pop up in that you need to add the country code or "___90%" so the search is done. Also, if you use OR, then if there is at least one match, it will count 3 values, if AND is used, then you need a complete match in 3 columns.
SELECT ID, tel_balance1 + tel_balance2 + tel_balance3
FROM telephone WHERE tel1 LIKE "38090%" OR tel2 LIKE "38090%" OR tel3 LIKE "38090%"
CREATE TABLE `telephone` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`tel1` bigint(20) UNSIGNED NULL DEFAULT NULL,
`tel2` bigint(20) UNSIGNED NULL DEFAULT NULL,
`tel3` bigint(20) UNSIGNED NULL DEFAULT NULL,
`tel_balance1` decimal(65, 2) NULL DEFAULT 0,
`tel_balance2` decimal(65, 2) NULL DEFAULT 0,
`tel_balance3` decimal(65, 2) NULL DEFAULT 0,
`birthday` date NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
)
Answer the question
In order to leave comments, you need to log in
You can do this:
SELECT
ID,
tel_balance1 * (tel1 LIKE "38090%") +
tel_balance2 * (tel2 LIKE "38090%") +
tel_balance3 * (tel3 LIKE "38090%") AS balance_38090
FROM
telephone
WHERE
tel1 LIKE "38090%"
OR tel2 LIKE "38090%"
OR tel3 LIKE "38090%"
;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question