是否可以在查询中将两个数字相乘以进行分页MYSQL查询?
for z in zipcodes:
# gets the website to find the closest big city
res = requests.get('https://www.travelmath.com/cities-near/' + z)
soup = bs4.BeautifulSoup(res.text, 'html.parser')
elems = soup.select('#EchoTopic > div:nth-child(1) > ul:nth-child(8) > li:nth-child(1) > a:nth-child(2)')
place = elems[0].text.strip()
city, state = place.split(',')
# uses the city found above to find the text in the webpage and the region it corresponds to
newres = requests.get('https://www.almanac.com/weather/longrange')
newsoup = bs4.BeautifulSoup(newres.text, 'html.parser')
newelems = newsoup.find('td', text=city).parent.parent
alltext = newelems.text
region = find_region(alltext) # helper regex function I wrote
# appends the zip code, city, and region to the DataFrame
regions.append([z, city, region])
当我尝试在SQL中动态地对这两个数字进行乘法运算时,在乘法部分遇到了关于我的语法的错误。在查询中不能乘以数字吗?
我的完整查询
SELECT * FROM Posts
LIMIT $itemsPerPage
OFFSET ($pageNumber * $itemsPerPage)
例如,上面的SELECT com_id, time_stamp, comBy, post_id, comContent, IFNULL(likes, 0) - IFNULL(dislikes, 0) AS likeResult, profile_pic FROM Comments C
# Adding Likes
LEFT JOIN (SELECT likedCom_id, COUNT(*) AS likes FROM Comment_Likes
GROUP BY likedCom_id) Cl ON C.com_id=Cl.likedCom_id
LEFT JOIN (SELECT dislikedCom_id, IFNULL(COUNT(*),0) AS dislikes FROM Comment_Dislikes
GROUP BY dislikedCom_id) Cd ON C.com_id=Cd.dislikedCom_id
LEFT JOIN (SELECT user_id, profile_pic FROM Users) U
ON U.user_id = C.comBy
WHERE post_id = '10'
ORDER BY time_stamp DESC
LIMIT 5
OFFSET (0 * 5)
代码分别表示pageNumber和itemsPerPage变量。
我在HeidiSQL上遇到有关我的查询的错误:
答案 0 :(得分:2)
OFFSET
参数是标量值,提供数字文字或单个绑定值。
如果需要进行乘法运算,则可以在客户端中进行乘法运算,并将结果作为单个参数提供给OFFSET
。
似乎代码正在动态生成SQL语句。我们可以做这样的事情:
$sql = "SELECT ... OFFSET " . (int)($pageNumber * $itemsPerPage) ;
或
$offset = $pageNumber * $itemsPerPage ;
$sql = "SELECT ... OFFSET " . (int)$offset ;
答案 1 :(得分:1)
您需要限制,并且不能使用计算来通过限制或偏移量。.
(您可以使用dinamic sql)
在select sintax中,OFFSET是LIMIT子句中的一个子句
https://dev.mysql.com/doc/refman/8.0/en/select.html#id4651990
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
my_valuse = pageNumber * itemsPerPage;
'SELECT * FROM Posts
LIMIT 100 OFFSET ' + my_value
,您应该使用
'SELECT *
FROM Posts
ORDER BY your_col
LIMIT 100 OFFSET ' + my_value