我的MySQL表格如下:
'id' 'name' 'type'
1 name1 A
2 name2 B
3 name3 A
4 name4 C
5 name5 C
如何在NOTORM PHP中获得所有不同的类型,即A,B,C。
答案 0 :(得分:4)
API文档不会以直观的方式覆盖它,但您只需使用按功能分组来获取不同的值:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ESBAMPortal.DataLayer;
using ESBAMPortal.Models;
using PagedList;
using ESBAMPortal.DomainClasses.BAM;
using System.IO;
namespace ESBAMPortal.Controllers
{
public class SupplierStockUpdateController : Controller
{
private IRepository<SupplierStockUpdate> SupplierStockUpdateRepository;
public SupplierStockUpdateController(BAMContext context)
{
this.SupplierStockUpdateRepository = new EFRepository<SupplierStockUpdate>(context);
}
private string TranslateStatusFilter(int status)
{
string ret = "";
switch (status)
{
case 2:
ret = "Pending";
break;
case 3:
ret = "Complete";
break;
}
return ret;
}
// GET: /SupplierStock/
public ActionResult Index(string sortOrder, int? page, int? itemsPerPage, SupplierStockUpdateViewModel vm)
{
string statusFilter = TranslateStatusFilter(vm.SelectedStatusId);
ViewBag.CurrentItemsPerPage = itemsPerPage;
ViewBag.TimeReceivedSort = String.IsNullOrEmpty(sortOrder) ? "timeReceived" : "";
ViewBag.SupplierSort = sortOrder == "supplier" ? "supplier_desc" : "supplier";
var query = this.SupplierStockUpdateRepository.GetAll();
switch (sortOrder)
{
case "timeReceived":
query = query.OrderBy(f => f.TimeReceived);
break;
case "supplier":
query = query.OrderBy(f => f.SupplierCode);
break;
case "supplier_desc":
query = query.OrderByDescending(f => f.SupplierCode);
break;
default:
query = query.OrderByDescending(f => f.TimeReceived);
break;
}
if (statusFilter == "Pending" || statusFilter == "Complete")
{
query = query.Where(x => x.ImportStatus == statusFilter);
}
//shove results into view model
List<SupplierStockUpdateViewModel> SupplierStockUpdatesVM = new List<SupplierStockUpdateViewModel>();
DateTime start = new DateTime();
DateTime end = new DateTime();
foreach (var item in query.ToList())
{
SupplierStockUpdateViewModel SupplierStockUpdateVM = new SupplierStockUpdateViewModel();
SupplierStockUpdateVM.ActivityID = item.ActivityID;
SupplierStockUpdateVM.InterchangeId = item.InterchangeId;
SupplierStockUpdateVM.TimeReceived = item.TimeReceived;
SupplierStockUpdateVM.TimeProductLookupRequested = item.TimeProductLookupRequested;
SupplierStockUpdateVM.TimeProductLookupResponse = item.TimeProductLookupResponse;
SupplierStockUpdateVM.TimeInsertRequested = item.TimeInsertRequested;
SupplierStockUpdateVM.TimeInsertResponse = item.TimeInsertResponse;
SupplierStockUpdateVM.SupplierCode = item.SupplierCode;
SupplierStockUpdateVM.RowsInserted = item.RowsInserted;
SupplierStockUpdateVM.TimeOfException = item.TimeOfException;
SupplierStockUpdateVM.ReceivedFilename = item.ReceivedFilename;
SupplierStockUpdateVM.ImportStatus = item.ImportStatus;
SupplierStockUpdateVM.ImportErrors = item.ImportErrors;
start = (item.TimeReceived == null) ? DateTime.MinValue : (System.DateTime)item.TimeReceived;
if (item.TimeOfException == null)
{
SupplierStockUpdateVM.TimeOfExceptionString = "NA";
if (item.TimeInsertResponse != null)
{
end = (System.DateTime)item.TimeInsertResponse;
}
else
{
//no exception but process still running so give a duration of 0
end = start;
}
}
else
{
SupplierStockUpdateVM.TimeOfExceptionString = item.TimeOfException.ToString();
end = (System.DateTime)item.TimeOfException;
}
if (start == DateTime.MinValue)
{
SupplierStockUpdateVM.TotalDuration = 0;
}
else
{
SupplierStockUpdateVM.TotalDuration = (end - start).TotalSeconds;
}
SupplierStockUpdatesVM.Add(SupplierStockUpdateVM);
}
int pageSize = (itemsPerPage ?? 10);
int pageNumber = (page ?? 1);
return View(SupplierStockUpdatesVM.ToPagedList(pageNumber, pageSize));
}
}
}
设置GROUP BY和HAVING
运行不同的内容时(不是在数据库级别,但在输出级别相同)。
没有确切部分的锚点,但它在这里:http://www.notorm.com/#api
答案 1 :(得分:2)
使用sql查询
SELECT DISTINCT type FROM yourtable
使用notORM API:
('SELECT DISTINCT type FROM ') $table
另一种可能性是使用工作arround:
$table->select('DISTINCT type')