我有一个表格,如下所示,我希望通过id得到记录,如果值b不存在则值为b,然后是值b。我在表格中有一个日期卷,我可以按日期使用订单。
我试图从移动设备发布屏幕图像,但是无法附加。 也无法发布我试过的sql,因为这里是官方不允许的。
我试图解释这个问题在下面
Persid. Value. Date
1 a
1 a
1 a
2 b
2 a
2 a
预期结果
1 a. Latest date
2 b any date
我尝试在id和value列上使用group by来计算,但我无法理解如何使用该计数来过滤掉记录。
非常感谢任何帮助:)
答案 0 :(得分:0)
您可以使用namespace i.model
{
public class CompanyRecords
{
public int? id { get; set; }
public int? user_id { get; set; }
public string product_name { get; set; }
public string Keywords { get; set; }
public string category { get; set; }
public string description { get; set; }
public string modules { get; set; }
}
[enter image description here][1]}
:
public List<CompanyRecords> GetProducts(string data,string idd, SearchModel searchSch)
{ using (MySqlConnection conn = GetConnection())
{
conn.Open();
List<CompanyRecords> results = conn.Query<CompanyRecords>("SELECT * FROM product WHERE category LIKE '%" + data + "%' OR Keywords LIKE '%" + data + "%' OR product_name LIKE '%" + data + "%' OR description LIKE '%" + data + "%' OR modules LIKE '%" + data + "%'OR metatags LIKE '%" + data + "%'OR geographical_focus LIKE '%" + data + "%'").ToList();
//if (idd == "Category")
if (!string.IsNullOrEmpty(searchSch.category))
{
//List<CompanyRecords> result = conn.Query<CompanyRecords>("SELECT * FROM product WHERE category LIKE '%" + data + "%'").ToList();
List<CompanyRecords> result = conn.Query<CompanyRecords>(results.Where(x=>x.category.Contains(searchSch.category))).ToList();
return result;
}
else if (idd == "Keywords")
{
List<CompanyRecords> result = conn.Query<CompanyRecords>("SELECT * FROM product WHERE Keywords LIKE '%" + data + "%'").ToList();
return result;
}
else if (idd == "Modules") {
List<CompanyRecords> result = conn.Query<CompanyRecords>("SELECT * FROM product WHERE modules LIKE '%" + data + "%'").ToList();
return result;
}
return results;
//List<CompanyRecords> results = conn.Query<CompanyRecords>("SELECT * FROM product WHERE category LIKE '%" + data + "%'OR description LIKE '%" + data + "%' OR modules LIKE '%" + data + "%'OR metatags LIKE '%" + data + "%'OR geographical_focus LIKE '%" + data + "%'OR target_job_titles LIKE '%" + data + "%'OR target_industry_type LIKE '%" + data + "%'OR desktop_web_both LIKE '%" + data + "%'OR dept_user_type LIKE '%" + data + "%'OR semantic LIKE '%" + data + "%'OR cognitive LIKE '%" + data + "%'OR target_campany_size LIKE '%" + data + "%'").ToList();
//if (results != null)
//{ return results; }
//return results;
}
}
这将获取任何值的最新日期。请注意,row_number()
是“b”出现在“a”之前。如果您的值不按字母顺序排序,则可以使用select id, value, date
from (select t.*,
row_number() over (partition by persid
order by value desc, date desc
) as seqnum
from t
) t
where seqnum = 1;
或其他机制来设置正确的排序顺序。