我想解决的问题是,我想计算两个或更多表中相同用户名出现的次数。
我想要搜索的用户名需要从$_SESSION['username']
First Table
| id | username | date |
| 1 | Mart |28-5-13|
| 2 | farm |27-5-13|
| 3 | Mart |29-5-13|
Second Table
| 1 | Mart |22-5-13|
| 1 | Mart |25-5-13|
| 1 | farm |24-5-13|
让我们看看$_SESSION['username'] = 'Mart'
如何统计两个表中出现相同用户名“Mart”的次数?
有点像吗?
$bloo = mysql_query('SELECT COUNT("username") FROM events AND Good WHERE username = "'. mysql_real_escape_string($_SESSION['username']) . '"');
while ($row2 = mysql_fetch_array($bloo))
或者我走错了路?
任何帮助表示赞赏!谢谢
答案 0 :(得分:1)
尝试使用first_table
和second_table
是您提到的2个表(“抱歉,1个数据库2表!”):
SELECT
SUM(c)
FROM (
SELECT COUNT(*) as c FROM first_table WHERE username = 'Mart'
UNION ALL
SELECT COUNT(*) as c FROM second_table WHERE username = 'Mart'
) as tmp
答案 1 :(得分:0)
所有数据库都在相同的服务器中?因为我认为分离服务器的最佳解决方案是使用Web服务......但如果不是,则只需要从两个查询中添加两个值。
Connect to multiple mysql DataBases
$dbh1 = mysql_connect($hostname, $username, $password);
$dbh2 = mysql_connect($hostname, $username, $password, true);
mysql_select_db('database1', $dbh1);
mysql_select_db('database2', $dbh2);
$res1 = mysql_query('select count(*) from tablename WHERE username="username"', $dbh1);
$res2 = mysql_query('select count(*) from tablename WHERE username="username', $dbh2);
$val1 = mysql_fetch_row($res1);
$val2 = mysql_fetch_row($res2);
$sum = $val1[0] + $val2[0];