我希望按日期排序mysql中的2个表的数据

时间:2017-12-04 16:50:06

标签: php mysql

我想从msql中的两个表中获取数据,按日期排序。

$sql = "
SELECT 
    items.*, invoice.* 
FROM 
    items 
JOIN 
    invoice 
ON 
    items.user_id = invoice.buyer_id 
WHERE 
    items.user_id = '$user_id'" 
LIMIT 
    10 
ORDER BY 
    date;
";

1 个答案:

答案 0 :(得分:0)

尝试:

$sql = "SELECT *";
$sql .= " FROM items, invoice";
$sql .= " WHERE items.user_id = invoice.buyer_id";
$sql .= " AND items.user_id = '$user_id'";
$sql .= " ORDER BY date DESC";
$sql .= " LIMIT 10";

最好将它用作预准备语句,而不是在SQL查询中包含变量以避免SQL注入。