我在哪里插入 ORDER BY 在我的 MYSQL 查询中 [英] Where do I insert the ORDER BY in my MYSQL query

查看:46
本文介绍了我在哪里插入 ORDER BY 在我的 MYSQL 查询中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的 SQL 语句中按ORDER BY prod_name"对结果进行排序,但我无法弄清楚让它工作.我试过

I need to have my results sorted by "ORDER BY prod_name" in my SQL statement but I cannot figure out get it to work. I tried after

$thisProduct .= " AND prod_type = 1 ORDER BY prod_name";

以及之后

$thisProduct .= " AND ID = '" . mysql_real_escape_string($_GET['product']) . "' ORDER BY prod_name";

但是我无法让我的结果正确排序.我是在错误的位置下订单还是我错误地查询了数据库?

But I cannot get my results to sort correctly. Am I placing the order by in the wrong spot or did I query the DB incorrectly?

提前谢谢你,我对 MYSQL 查询还是很陌生.

Thank you in Advance, I am still pretty new at MYSQL queries.

$thisProduct = "SELECT prod_name AS Name, days_span, CONCAT(LEFT(prodID,2),ID) AS ID,     geo_targeting FROM products WHERE status = 'Active' AND vendID = ".$resort['vendID'];
if (isset($_GET['product']) AND is_numeric($_GET['product'])) {
$thisProduct .= " AND ID = '" . mysql_real_escape_string($_GET['product']) . "'";
}
else {
    $thisProduct .= " AND prod_type = 1";
}
$thisProduct .= " LIMIT 1";

$getThisProduct = mysql_query($thisProduct);
if (!$getThisProduct/*  OR mysql_num_rows($getThisProduct) == 0 */) {
    header("HTTP/1.0 404 Not Found");
    require APP_PATH . '/404.html';
    die();
}

$thisProductData = mysql_fetch_assoc($getThisProduct);

推荐答案

你应该:

$thisProduct .= " ORDER BY prod_name";
$thisProduct .= "限制 1";

$thisProduct .= " ORDER BY prod_name";
$thisProduct .= " LIMIT 1";

(请注意,LIMIT 1 表示您只能获得一条记录).

(Note that the LIMIT 1 means you only get one record).

这篇关于我在哪里插入 ORDER BY 在我的 MYSQL 查询中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆