我需要创建一个查询来搜索users表中的单词,并返回在其字段中有用户名(用户名,名字,姓氏等)的用户。 但是,此查询应返回先前在所有用户中找到的,仅返回那些我的朋友。 表格如下:
用户
朋友
我做了这个查询,但它不起作用:
$sql = "SELECT u.*"
. "FROM (SELECT * "
. "FROM `users` "
. "WHERE (`username` LIKE '%".$value."%' OR "
. "`email` LIKE '%".$value."%' OR "
. "`first_name` LIKE '%".$value."%' OR "
. "`last_name` LIKE '%".$value."%'"
. ") ORDER BY ".$order.") u"
. "INNER JOIN `friend` ON (`u.username` = `friend.user_acc`)"
. "WHERE `friend.user_acc` = ".$username." AND `friend.status = 1`";
我试图清理查询(尝试制作一个字符'f')并直接运行它,但它不起作用。什么都不返回,什么时候应该返回一些记录。
SELECT u.*
FROM (
SELECT *
FROM `users`
WHERE (
`username` LIKE '%f%' OR
`email` LIKE '%f%' OR
`first_name` LIKE '%f%' OR
`last_name` LIKE '%f%' OR
`city` LIKE '%f%' OR
`gender` LIKE '%f%'
) ORDER BY `username`) u
INNER JOIN `friend` ON (u.`username` = friend.`user_acc`)
WHERE friend.`user_acc` = 'LP' AND friend.`status` = '1';
由于
答案 0 :(得分:0)
......有什么问题。
$sql = "
SELECT u.*
FROM users u
JOIN friend f
ON f.user_acc = u.username
WHERE ( username LIKE '%$value%' OR
email LIKE '%$value%' OR
first_name LIKE '%$value%' OR
last_name LIKE '%$value%'
)
AND f.user_acc = '$username'
AND f.status = 1
ORDER
BY $order;
";
...