我想做一个php点击任务。条件时我需要mysql循环标题和url。
我的情况是(显示t2.title,t2.url
)userid
时t2.id
click_log
t2.clickdate = $todaydate
click_log
。{/ 1}}
我有2个表,首先是t1
AS +--------+-----------+----------------+---------------+
| ID | uid | clickdate | clickid |
+--------+-----------+----------------+---------------+
| 1 | 1 | 1452009600 | 1 |
| 2 | 1 | 1452009600 | 2 |
| 3 | 1 | 1452009600 | 3 |
| 4 | 1 | 1452096000 | 1 |
| 5 | 1 | 1452096000 | 2 |
+-----------------------------------------------------+
t1.clickdate
timestamp
是GMT + 8 t1.clickid
,t2.id
= click_title
第二个表格为t2
AS +---------+---------+--------------+-------------+------------+
| ID | title | url | status | other |
+---------+---------+--------------+-------------+------------+
| 1 | Helo | http://xxx | 1 | |
| 2 | World | http://www | 1 | |
| 3 | Good | http://ggg | 1 | |
| 4 | Morning | http://aaa | 2 | |
+-------------------------------------------------------------+
$_G['timestamp'] = time(); //my server is GMT+8
$todaydate = date("Y-m-d",($_G['timestamp'])); //output 2016-01-07
$todaytimestamp = strtotime($todaydate); //output 1452096000
$_G['uid'] = intval($_GET['userid']); //this is user id
我的编码定义:
$queryJN = DB::query("SELECT t1.*,t2.title,t2.url FROM ".DB::table('click_log')." t1 LEFT JOIN ".DB::table('click_title')." t2 ON (t1.clickid = t2.id) WHERE (how to write the codition?)");
while($rowJN = DB::fetch($queryJN)){
$jn_list[] = $rowJN;
}
我这样查询:
$queryJNbux = DB::query("SELECT t1.*,t2.title,t2.url FROM ".DB::table('click_log')." t1 LEFT JOIN ".DB::table('click_title')." t2 ON (t1.clickid = t2.id) WHERE $_G[uid] IS NULL AND t2.id IS NULL AND t2.status = '1' AND t1.clickdate != '$todaydate'");
我试着像:
t1.id
但是无法运行,假设title
3 url
和t1.id 3
会显示t1.clickid
因t1.clickdate
1452096000
DB::query
{1}}
对于安全问题,{1}在1个查询中无法执行select * from
两次。如果没有select * from
两次或更多,请帮助我,谢谢。
昨天整天我都在思考其他方式,之后我尝试使用DB::fetch
,它可以工作,但我不知道查询是否安全,或者可能导致其他问题。也许有人可以改进这个查询。
$queryJN = DB::query("SELECT * FROM ".DB::table('click_title')." WHERE status = '1'");
while($rowJN = DB::fetch($queryJN)){
$rowexisting = DB::query("SELECT * FROM ".DB::table('click_log')." WHERE uid = '$_G[uid]' AND clickid = '$rowJN[id]' AND clickdate = '$todaydate'");
if (mysql_num_rows($rowexisting) == '0'){
$jn_list[] = $rowJN;
};
}
答案 0 :(得分:0)
您可以使用此查询...
我在这里使用了NOT IN
和EXISTS
来实现您的条件......
SELECT t2.title,t2.url
FROM ".DB::table('click_log')." t1 JOIN ".DB::table('click_title')." t2
WHERE $_G[uid]!=t2.uid AND t2.ID!=t1.ID AND t1.clickdate ='$todaydate'
我已更新了我的查询,因为您在此处看不到嵌套查询。
有很多方法可以编写子查询。
但希望这个可以解决你的问题。
答案 1 :(得分:0)
只要允许列为NULL,就可以使用WHERE它为NULL。 你从2个表中提取数据,所以没有RIGHT JOIN和LEFT JOIN。你可以使用JOIN或INNER JOIN。
SELECT t1.*, t2.title, t2.url
FROM t1
INNER JOIN t2
ON t1.clickid = t2.ID
WHERE t1.uid = NULL AND WHERE t1.clickid = NULL
OREDER BY t1.clickdate ASC;
或使用COUNT,其中某列的计数为0
选择Count NOT NULL t1.uid