简体   繁体   English

在php中查询和排序数据

[英]querying and sorting data in php

I am creating project using php and showing data on my page using mysql query.Now i need to fetch everytime 10 records and sorted according to image comes first.我正在使用 php 创建项目并使用 mysql 查询在我的页面上显示数据。现在我需要每次获取 10 条记录并首先根据图像进行排序。

What i want to achieve is:我想要实现的是:

Fetch data from DB and sort by image field, means the data who have valid image path and not null.从数据库中获取数据并按图像字段排序,表示具有有效图像路径且不为空的数据。

Also some images have path but the image does not present on that path also needs to be sorted还有一些图像有路径,但图像不存在于该路径上也需要排序

Here is the Query:这是查询:

$producteviews=  DB::select("SELECT eqr.buyerName As pBuyer,eqr.userImage, eqr.description As reviewDescription,eq.* 
FROM equipments_review As eqr 
     INNER JOIN equipments As eq ON eq.id=eqr.equipment_id 
Where eqr.status = '1' ORDER by  eqr.created_at DESC limit ".$id.",10");

This query getting the data according to date and gives 10 records, bu i need to get and sort the data according to valid conditions此查询根据日期获取数据并给出 10 条记录,但我需要根据有效条件获取和排序数据

In your select query, and an IS NOT NULL where condition.在您的选择查询中,以及一个IS NOT NULL where 条件。 Assuming that your image field is eqr.userImage , then it would look like this:假设您的图像字段是eqr.userImage ,那么它看起来像这样:

$producteviews=  DB::select("SELECT eqr.buyerName As pBuyer,eqr.userImage, eqr.description As reviewDescription,eq.* FROM equipments_review As eqr INNER JOIN equipments As eq ON eq.id=eqr.equipment_id Where eqr.status = '1' AND eqr.userImage IS NOT NULL ORDER by  eqr.created_at DESC limit ".$id.",10");

In the above query, I have copied the statement in question and added the IS NOT NULL to show usage.在上面的查询中,我复制了有问题的语句并添加了IS NOT NULL以显示用法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM