This is a COUNT query I'm using to display pages:
$sql = "
SELECT SUM(num) as num FROM (
SELECT
COUNT(URL) AS num
, 'World' AS GoSection
, 'GW' AS MySite
FROM gw_geog
WHERE URL = :MyURL AND G1 = 1
UNION ALL
SELECT
COUNT(URL) AS num
, 'World' AS GoSection
, 'GW' AS MySite
FROM gw_geog_political
WHERE URL = :MyURL
) AS X";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();
I want to make two changes:
1) Delete WHERE URL = :MyURL from each line and incorporate it into a single line at the end of the query
2) Zap duplicates by adding LIMIT 1
This is what I'm working with right now.
$sql = "SELECT SUM(num) as num FROM (
SELECT COUNT(URL) AS num, 'World' AS GoSection, 'GW' AS MySite FROM gw_geog WHERE URL = :MyURL AND G1 = 1
UNION ALL
SELECT COUNT(URL) AS num, 'World' AS GoSection, 'GW' AS MySite FROM gw_geog_political WHERE URL = :MyURL
) AS X
WHERE X.URL LIKE :MyURL LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();
However, I get the error message Unknown column X.SUM (or X.URL, etc.).
Can someone show me the correct way to do this?