I am having a problem when doing a left join with the “parked_stats” table below. I think it is because there is no data in that table. Would that cause no rows to be returned? If so, how can I get around that?
select UNIX_TIMESTAMP(dt_auctions.end_date) - UNIX_TIMESTAMP() as time_left, sum(dt_parked_stats.uniques) as website_visitors, dt_currencies.symbol, dt_auctions.end_date, dt_auctions.paid_auction, dt_auctions.high_bid, dt_domains.id, dt_domains.domain, dt_domains.bin_price, dt_domains.minimum_offer, dt_domains.views, dt_domains.previous_offers, dt_domains.currency from dt_domains left join dt_currencies on dt_currencies.code = dt_domains.currency left join dt_auctions on dt_auctions.domain_id = dt_domains.id left join dt_parked_stats on dt_parked_stats.domain = dt_domains.domain where active='1' order by dt_domains.domain
Okay great, seems to work now. One more quick question: how can I order by website_visitors column that I create within this query? I tried and it just gives an error, here’s my query now:
select UNIX_TIMESTAMP(dt_auctions.end_date) - UNIX_TIMESTAMP() as time_left,
if(sum(dt_parked_stats.uniques) is null, '--', dt_parked_stats.id) as website_visitors,
dt_currencies.symbol,
dt_auctions.end_date,
dt_auctions.paid_auction,
dt_auctions.high_bid,
dt_domains.id,
dt_domains.domain,
dt_domains.bin_price,
dt_domains.minimum_offer,
dt_domains.views,
dt_domains.previous_offers,
dt_domains.currency from dt_domains
inner join dt_currencies on dt_currencies.code = dt_domains.currency
left outer join dt_auctions on dt_auctions.domain_id = dt_domains.id
left outer join dt_parked_stats on dt_parked_stats.domain = dt_domains.domain
where dt_domains.active='1'
group by dt_domains.id order by website_visitors asc
i said it was okay for the right table to have no data in a LEFT OUTER JOIN
i mean, that’s why you need a LEFT OUTER JOIN – to return a given row from the left table even if there is no matching row for it in the right table
that said, your first join should be INNER, since i am going to bet that there is no value in dt_domains.currency that isn’t in dt_currencies.code
on the other hand, the second and third joins should be LEFT OUTER, since i imagine it might be possible for there to be domains that have no auction, as well as domains that have no parked stats
Im now using the following query but still no results. Yet if I drop the right join, it works fine.
select UNIX_TIMESTAMP(dt_auctions.end_date) - UNIX_TIMESTAMP() as time_left,
sum(dt_parked_stats.uniques) as website_visitors,
dt_currencies.symbol,
dt_auctions.end_date,
dt_auctions.paid_auction,
dt_auctions.high_bid,
dt_domains.id,
dt_domains.domain,
dt_domains.bin_price,
dt_domains.minimum_offer,
dt_domains.views,
dt_domains.previous_offers,
dt_domains.currency from dt_domains
left outer join dt_currencies on dt_currencies.code = dt_domains.currency
left outer join dt_auctions on dt_auctions.domain_id = dt_domains.id
right join dt_parked_stats on dt_parked_stats.domain = dt_domains.domain
where dt_domains.active='1' group by dt_domains.id order by dt_domains.domain
okay, then your WHERE clause is fine, although you should probably qualify the active column with its table name as general good practice in query writing
p.s. if you want people to look at your query, please apply some formatting
SELECT UNIX_TIMESTAMP(dt_auctions.end_date) -
UNIX_TIMESTAMP() AS time_left
, sum(dt_parked_stats.uniques) AS website_visitors
, dt_currencies.symbol
, dt_auctions.end_date
, dt_auctions.paid_auction
, dt_auctions.high_bid
, dt_domains.id
, dt_domains.domain
, dt_domains.bin_price
, dt_domains.minimum_offer
, dt_domains.views
, dt_domains.previous_offers
, dt_domains.currency
FROM dt_domains
LEFT
JOIN dt_currencies
ON dt_currencies.code = dt_domains.currency
LEFT
JOIN dt_auctions
ON dt_auctions.domain_id = dt_domains.id
LEFT
JOIN dt_parked_stats
ON dt_parked_stats.domain = dt_domains.domain
WHERE active='1'
ORDER
BY dt_domains.domain
coding everything on one humoungously long line is user-hostile
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/user/public_html/smarty/libs/plugins/function.return_paged_results.php on line 81
Ahhh I see…but is there no way for me to use it in the WHERE clause? It’s kind of imperative that I do use it in the WHERE clause otherwise it deems my sql query useless.