[英]Getting mysql data where all rows have same column value
I want to get all rows with same part value, like 我想获得所有具有相同零件价值的行,例如
1. lesson 1 part=1
2. lesson 1.1 part=1
3. lesson 2 part=2
I want to get lesson 1 and 1.1 because their part is equal 我想上课1和1.1,因为它们的部分相等
wl - What lesson from math
lessons - first table
ls - second table
lid - lesson id from lessons table
Just to get non seen lessons.
"SELECT * FROM lessons
WHERE wl='$math'
AND id IN(SELECT lid FROM ls WHERE fid='$user' AND seen='0')
GROUP BY part LIMIT 2";
As this is just for you to look up the records, it probably suffices to get a list of IDs for each part, for which you can use GROUP_CONCAT
: 因为这只是为了查找记录,所以足以获取每个零件的ID列表,您可以使用GROUP_CONCAT
:
SELECT part, GROUP_CONCAT(id) as ids
FROM lessons
WHERE wl='$math'
AND id IN(SELECT lid FROM ls WHERE fid='$user' AND seen='0')
GROUP BY part
HAVING COUNT(*) > 1;
If you wanted a row per part and ID that would be more difficult. 如果要每个零件和ID行,那将更加困难。 Such problems are usually solved with analytic functions, but MySQL doesn't feature them yet. 此类问题通常可以通过解析函数解决,但MySQL尚未提供这些功能。 This makes it necessary to write part of the query twice. 这使得有必要写两次查询的一部分。
This is how to get the unseen lessons for the user: 这是为用户提供看不见的课程的方法:
SELECT *
FROM lessons
WHERE wl='$math'
AND id IN(SELECT lid FROM ls WHERE fid='$user' AND seen='0');
And this is how you get the parts that appear more than once in the results: 这就是您如何获得在结果中多次出现的零件的方法:
SELECT part
FROM lessons
WHERE wl='$math'
AND id IN(SELECT lid FROM ls WHERE fid='$user' AND seen='0')
GROUP BY part
HAVING COUNT(*) > 1;
The two combined: 两者结合:
SELECT *
FROM lessons
WHERE wl='$math'
AND id IN(SELECT lid FROM ls WHERE fid='$user' AND seen='0')
AND part IN
(
SELECT part
FROM lessons
WHERE wl='$math'
AND id IN(SELECT lid FROM ls WHERE fid='$user' AND seen='0')
GROUP BY part
HAVING COUNT(*) > 1
);
For your condition "I need 2 same because they can have only 2 same" - use the following query: 对于您的条件“我需要2个相同的字符, 因为它们只能具有2个相同的字符” -使用以下查询:
SELECT
id, part
FROM
lessons
WHERE
id REGEXP
(SELECT
group_concat(id separator '|')
FROM
lessons
WHERE
wl='$math'
AND id IN (SELECT lid FROM ls WHERE fid='$user' AND seen='0')
GROUP BY part
HAVING count(part) = 2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.