我的表是:
id user date
---|-----|------------|
1 | ab | 2011-03-04 |
2 | ab | 2016-03-04 |
3 | cd | 2009-03-04 |
4 | cd | 2013-03-03 |
5 | ef | 2009-03-03 |
我需要选择每个不同的用户:
“更新”>日期超过当前日期
结果将是:
id user date
---|-----|------------|
2 | ab | 2016-03-04 |
5 | ef | 2016-03-03 |
“过期”>日期少于当前日期
结果将是:
id user date
---|-----|------------|
3 | cd | 2009-03-04 |
我试过了:
SELECT t1.* FROM tmp t1
WHERE t1.date = (SELECT MAX(t2.date)
FROM tmp t2 WHERE t2.user = t1.user
AND YEAR(MAX(t2.date))<(YEAR(CURRENT_TIMESTAMP)-1))
order by user asc
它不起作用。结果集有0行。 有任何想法吗?谢谢。它会对我有所帮助..
答案 0 :(得分:0)
您的日期采用MM/dd/YYYY
的特殊格式,在尝试此处给出的任何答案时,这可能会给您带来麻烦。您应该将04/03/2011
转换为2011-04-03
。话虽如此,假设您的date
列确实是MySQL日期类型,那么以下查询将为您提供所有不同的“更新”用户:
SELECT t1.id, t1.user, t1.date, 'update' AS status
FROM antivirus t1
INNER JOIN
(
SELECT user, MAX(date) AS date
FROM antivirus
GROUP BY user
HAVING MAX(date) >= CURDATE()
) t2
ON t1.user = t2.user AND t1.date = t2.date
请注意,在我的小提琴中,我必须修改样本表中日期输入的格式。
如果您还希望同一查询中的“已过期”用户,则可以添加UNION ALL
以及以下内容:
SELECT t1.id, t1.user, t1.date, 'expired' AS status
FROM antivirus t1
INNER JOIN
(
SELECT user, MAX(date) AS date
FROM antivirus
GROUP BY user
HAVING MAX(date) < CURDATE()
) t2
ON t1.user = t2.user AND t1.date = t2.date
答案 1 :(得分:0)
对于大于当前日期的日期:
group by
实际上,我认为你想要一个select user
from t
group by user
having max(`date`) >= curdate();
:
def distorted_inputs(data_dir, batch_size):
"""Construct distorted input for CIFAR training using the Reader ops.
Args:
data_dir: Path to the CIFAR-10 data directory.
batch_size: Number of images per batch.
Returns:
images: Images. 4D tensor of [batch_size, IMAGE_SIZE, IMAGE_SIZE, 3] size.
labels: Labels. 1D tensor of [batch_size] size.
"""
filenames = [os.path.join(data_dir, 'data_batch_%d.bin' % i)
for i in xrange(1, 6)]
for f in filenames:
if not gfile.Exists(f):
raise ValueError('Failed to find file: ' + f)
# Create a queue that produces the filenames to read.
filename_queue = tf.train.string_input_producer(filenames)
# Read examples from files in the filename queue.
read_input = read_cifar10(filename_queue)
reshaped_image = tf.cast(read_input.uint8image, tf.float32)
distorted_image = reshaped_image
height = IMAGE_SIZE
width = IMAGE_SIZE
# Image processing for training the network. Note the many random
# distortions applied to the image.
# Randomly crop a [height, width] section of the image.
# distorted_image = tf.image.random_crop(reshaped_image, [height, width])
# Randomly flip the image horizontally.
# distorted_image = tf.image.random_flip_left_right(distorted_image)
# Because these operations are not commutative, consider randomizing
# randomize the order their operation.
distorted_image = tf.image.random_brightness(distorted_image,
max_delta=63)
distorted_image = tf.image.random_contrast(distorted_image,
lower=0.2, upper=1.8)
# Subtract off the mean and divide by the variance of the pixels.
float_image = tf.image.per_image_whitening(distorted_image)
# Ensure that the random shuffling has good mixing properties.
min_fraction_of_examples_in_queue = 0.4
min_queue_examples = int(NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN *
min_fraction_of_examples_in_queue)
print ('Filling queue with %d CIFAR images before starting to train. '
'This will take a few minutes.' % min_queue_examples)
# Generate a batch of images and labels by building up a queue of examples.
return _generate_image_and_label_batch(float_image, read_input.label,
min_queue_examples, batch_size)
这也适用于较早的日期。
答案 2 :(得分:0)
有几点意见:
&#34;它没有工作&#34;并不是对您观察到的行为的描述。它没有描述查询是否正在执行并返回意外结果,或者查询是否返回错误。
date
值显示的值采用奇怪的非MySQL格式。 DATE
值的标准格式为YYYY-MM-DD
。这让我们想知道名为date
的列的实际数据类型是什么。如果它是一个字符串,包含一年中的月份和日期,则会在比较中出现问题。字符串比较是从左到右逐个字符完成的。
在此上下文中使用YEAR()
函数非常奇怪。你说你想比较&#34; date&#34;值。 YEAR
函数从日期中提取年份值。所以看起来您的查询只会比较一年。
同样,如果YEAR()
函数的参数不是DATE
数据类型,则会将该参数隐式转换为DATE
。 ..并且为了使隐式转换正常工作,字符串将需要采用&#39; YYYY-MM-DD&#39;等格式。
此外,子查询看起来在MAX()
子句中使用聚合函数WHERE
。那是无效的。访问行时,将评估WHERE
子句中的谓词。在访问行之后,聚合函数的值将不可用,并且执行group by操作。如果您需要聚合条件,则可以进入HAVING
子句。
答案 3 :(得分:0)
感谢@TimBiegeleisen
的回答..
我已经尝试过这段代码而且它有效..
此代码用于显示“更新”行:
select a.* from tmp a
inner join
(select user, max(date) as date from tmp group by user)
b on a.user=b.user and a.date=b.date
where year(a.date +interval 1 year) >= year(current_date())
order by a.user
这用于显示'过期'行:
select a.* from tmp a
inner join
(select user, max(date) as date from tmp group by user)
b on a.user=b.user and a.date=b.date
where year(a.date +interval 1 year) < year(current_date())
order by a.user
我希望我的问题和所有答案能够帮助解决其他问题。