在MYSQL和PHP中显示值直到第一个空格

在MYSQL和PHP中显示值直到第一个空格,php,mysql,mysqli,Php,Mysql,Mysqli,我试图在MYSQL和PHP中显示第一个空格之前的值。比如说, 1 items from 29 100 items from 10 在第一个示例中,我只想显示数字1,在第二个示例中,我只想显示数字100,依此类推。这是我的密码 $ordering = "SELECT t.id, t.cart_id, t.full_name, t.description, t.txn_date, t.grand_total, c.paid, c.shipped FROM transacti

我试图在MYSQL和PHP中显示第一个空格之前的值。比如说,

1 items from 29
100 items from 10
在第一个示例中,我只想显示数字1,在第二个示例中,我只想显示数字100,依此类推。这是我的密码

$ordering = "SELECT t.id, t.cart_id, t.full_name, t.description, t.txn_date, t.grand_total, c.paid, c.shipped
            FROM transactions t
            LEFT JOIN cart c ON t.cart_id = c.id
            WHERE c.paid = 1 AND c.shipped = 0
            ORDER BY t.txn_date
现在,我想用显示到第一个空格的代码替换
t.description
。我在互联网上搜索并找到了代码:

SELECT REVERSE(RIGHT(REVERSE(YourColumn), LEN(YourColumn) - CHARINDEX(' ', REVERSE(YourColumn))))
因此,我将
t.description
替换为:

$ordering = "SELECT t.id, t.cart_id, t.full_name, t.REVERSE(RIGHT(REVERSE(description), LEN(description) - CHARINDEX(' ', REVERSE(description)))), t.txn_date, t.grand_total, c.paid, c.shipped
                FROM transactions t
                LEFT JOIN cart c ON t.cart_id = c.id
                WHERE c.paid = 1 AND c.shipped = 0
                ORDER BY t.txn_date

是这样吗?最好的方法是什么?

使用MySQL,最简单的方法是使用例如

如果第三个参数为正(
count
,在本例中为1),它将返回第二个参数出现的
count
左侧的所有内容(在本例中为空格)。所以这个调用返回第一个空格左边的所有内容:

100

在您的查询中,您将写入

$ordering = "SELECT t.id, t.cart_id, t.full_name, SUBSTRING_INDEX(t.description, ' ', 1) AS description, t.txn_date, t.grand_total, c.paid, c.shipped
            FROM transactions t
            LEFT JOIN cart c ON t.cart_id = c.id
            WHERE c.paid = 1 AND c.shipped = 0
            ORDER BY t.txn_date

你能说清楚点吗?第一个空格是什么意思?你能详细解释一下你的要求吗?“MYSQL中的第一个空间”这意味着什么?你也可以在php中这样做,比如
$array=explode(“”,$description,2)
您将获得100美元的
$array[0]
$ordering = "SELECT t.id, t.cart_id, t.full_name, SUBSTRING_INDEX(t.description, ' ', 1) AS description, t.txn_date, t.grand_total, c.paid, c.shipped
            FROM transactions t
            LEFT JOIN cart c ON t.cart_id = c.id
            WHERE c.paid = 1 AND c.shipped = 0
            ORDER BY t.txn_date