Mysql 使用变量选择列
也许这不是一个标题的最佳描述,但我真的想不出任何东西。 我想做的是,我可以创建一个查询,使用userID作为关系从列表中选择类型,然后选择城镇Mysql 使用变量选择列,mysql,sql,database,relational-database,Mysql,Sql,Database,Relational Database,也许这不是一个标题的最佳描述,但我真的想不出任何东西。 我想做的是,我可以创建一个查询,使用userID作为关系从列表中选择类型,然后选择城镇 TABLE listings saleID userID type description 1 23 clothing nice clothing 2 45 clothing More nice Clothing TABLE users userID country
TABLE listings
saleID userID type description
1 23 clothing nice clothing
2 45 clothing More nice Clothing
TABLE users
userID country county town
23 uk county townname1
24 uk county townname2
这些变量在url中设置为get-eg)?type=clothing&town=townname2
if (!isset($type)){
$query = "SELECT * FROM listings";
}
if (isset($type)) {
$query = "SELECT * FROM listings WHERE type = '{$type}'";
}
这是我一直坚持的一点,我想获得所有带有变量$type“town”的列表,然后从用户中选择带有变量$town的town
if (isset($town)) {
$query = "SELECT users.town listings.userID listings.type FROM listings
WHERE type = '{$type}'
INNER JOIN users
ON users.town = '{$town}'";
}
希望我已经解释清楚了,能够理解。
请帮我回答最后一个问题
SELECT u.town l.userID, l.type
FROM listings l
INNER JOIN users u on u.userID = l.userID
WHERE l.type = '{$type}'
AND u.town = '{$town}'
要进一步了解有关联接的更多信息,请访问以下链接:
作为旁注,如果变量的值来自外部,则查询易受攻击。请看下面的文章,了解如何预防它。通过使用
PreparedStatements
可以避免在值周围使用单引号
作为旁注,如果变量的值来自外部,则查询易受攻击。请看下面的文章,了解如何预防它。通过使用
PreparedStatements
可以避免在值周围使用单引号
SELECT b.town, a.userID, a.type
FROM listings a
INNER JOIN users b
ON a.userID = b.userID
WHERE a.type = '{$type}' AND
b.town = '{$town}'