即使表没有记录,我也希望返回两个默认列值。我正在使用以下查询(感谢How to SELECT DEFAULT value of a field):
SELECT DEFAULT(membership_credits) AS membership_credits,
DEFAULT(product_credits) AS product_credits
FROM (SELECT 1) AS dummy LEFT JOIN Users ON True LIMIT 1
但是我没有使用默认值,而是获得NULL:
membership_credits product_credits
NULL NULL
有什么问题?
编辑:
按照评论中的建议添加表架构:
CREATE TABLE Users (
user_id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
user_login VARCHAR(40) NOT NULL UNIQUE,
user_name VARCHAR(100) NOT NULL,
user_email VARCHAR(254) NOT NULL UNIQUE,
user_telephone VARCHAR(100) NOT NULL,
user_password VARCHAR(64) NOT NULL,
user_address VARCHAR(255) NOT NULL,
user_postal_code VARCHAR(100) NOT NULL,
user_district VARCHAR(100) NOT NULL,
user_country VARCHAR(100) NOT NULL,
user_tax_number VARCHAR(20) NOT NULL,
user_billing_email VARCHAR(254) NOT NULL,
company_description TEXT,
company_history TEXT,
company_products TEXT,
public_contact BINARY(1) NOT NULL,
user_active BINARY(1) NOT NULL DEFAULT '0',
user_key VARCHAR(255) NOT NULL,
user_registered TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
unread_messages INT UNSIGNED DEFAULT 0,
membership_credits INT UNSIGNED NOT NULL DEFAULT 0,
product_credits INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE INNODB CHARACTER SET utf8 COLLATE utf8_general_ci;
答案 0 :(得分:1)
如果您使用以下查询来创建表
CREATE TABLE `users` (
`user_id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`user_login` VARCHAR(40) NOT NULL,
`user_name` VARCHAR(100) NOT NULL,
`user_email` VARCHAR(254) NOT NULL,
`user_telephone` VARCHAR(100) NOT NULL,
`user_password` VARCHAR(64) NOT NULL,
`user_address` VARCHAR(255) NOT NULL,
`user_postal_code` VARCHAR(100) NOT NULL,
`user_district` VARCHAR(100) NOT NULL,
`user_country` VARCHAR(100) NOT NULL,
`user_tax_number` VARCHAR(20) NOT NULL,
`user_billing_email` VARCHAR(254) NOT NULL,
`company_description` TEXT NULL,
`company_history` TEXT NULL,
`company_products` TEXT NULL,
`public_contact` BINARY(1) NOT NULL,
`user_active` BINARY(1) NOT NULL DEFAULT '0',
`user_key` VARCHAR(255) NOT NULL,
`user_registered` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`unread_messages` INT(10) UNSIGNED NULL DEFAULT '0',
`membership_credits` INT(10) UNSIGNED NULL DEFAULT '0',
`product_credits` INT(10) UNSIGNED NULL DEFAULT '0',
PRIMARY KEY (`user_id`),
UNIQUE INDEX `user_login` (`user_login`),
UNIQUE INDEX `user_email` (`user_email`)
)
使用以下查询获取默认值
SELECT if(DEFAULT(membership_credits) is null,0,
DEFAULT(membership_credits))AS membership_credits,if(DEFAULT(product_credits)
is null,0,DEFAULT(product_credits) )AS product_credits FROM
(SELECT 1) AS dummy LEFT JOIN Users ON True LIMIT 1
您必须注意,在上面的创建查询membership_credits
和product_credits
已修改为允许空
答案 1 :(得分:1)
尝试此查询
SELECT COLUMN_DEFAULT FROM information_schema.columns
WHERE TABLE_SCHEMA = 'your_database_name' AND
TABLE_NAME = 'your_table_name' AND COLUMN_NAME = 'your_column_name';
在你的情况下
SELECT COLUMN_DEFAULT FROM information_schema.columns
WHERE TABLE_SCHEMA = 'test' AND
TABLE_NAME = 'users' AND
COLUMN_NAME in ('membership_credits','product_credits');
答案 2 :(得分:1)
这完全取决于您的列是否允许NULL
。
如果NULL
允许,则以下查询将非常有用,因此该表没有记录:
SELECT
DEFAULT(`membership_credits`) `membership_credits`,
DEFAULT(`product_credits`) `product_credits`
FROM (SELECT 1) `dummy`
LEFT JOIN `users` ON TRUE
LIMIT 1;
如果不允许NULL
,并且该表没有记录,您将获得上述查询的NULL
。在这种情况下需要查询:
SELECT
DEFAULT(`membership_credits`) `membership_credits`,
DEFAULT(`product_credits`) `product_credits`
FROM (SELECT *, COUNT(0)
FROM `users`) `users`;
<强>更新强>
在MySQL中要小心&gt; = 5.6不能以相同的方式运行并获得NULL值。
答案 3 :(得分:0)
使用以下查询:
选择 IF(COUNT(user_id)= 0, DEFAULT(membership_credits), membership _credits)AS membership_credits, IF(COUNT(user_id)= 0, DEFAULT(product_credits), product_credits)AS product_credits FROM (选择1)AS虚拟LEFT JOIN 用户启用
答案 4 :(得分:-1)
使用以下查询:
选择 IF(COUNT(user_id)= 0, DEFAULT(membership_credits), membership _credits)AS membership_credits, IF(COUNT(user_id)= 0, DEFAULT(product_credits), product_credits)AS product_credits 从 (选择1)AS虚拟 LEFT JOIN 用户启用