SQl query running to return the total count from the query.
this code works when running SQL with PHPmyAdmin
But on the page it is not displaying echo of the count ?
Not sure if I could have overlooked something here.
Many Thanks!
$sql2=mysql_query("SELECT count(*)
FROM main_table LEFT JOIN houses ON main_table.housenumber = houses.housenumber AND main_table.streetname = houses.streetname
WHERE main_table.city='1'
group by main_table.city ORDER BY average DESC, houseID DESC, reviewID DESC;");
while($row=mysql_fetch_array($sql2))
{
$count=$row['count'];
echo $count;;
}
Try this ....
$sql2=mysql_query("SELECT
COUNT(*) AS count
FROM
main_table
LEFT JOIN houses
ON main_table.housenumber = houses.housenumber
AND main_table.streetname = houses.streetname
WHERE main_table.city = '1'
GROUP BY main_table.city
ORDER BY average DESC,
houseID DESC,
reviewID DESC") ;
while($row=mysql_fetch_array($sql2))
{
$count=$row['count'];
echo $count;
}
You have mistake in your query, you are not adding count in select as aliases, and below in while you are using aliases . Try this.
name your column :
...mysql_query("SELECT count(*) as count....
add SELECT count(*) as 'count' from ...
try starting your query with :
SELECT count(*) as count ...
Related
I got this Query:
$query = mysql_query("SELECT arbejdsopgave.*,
DATE_FORMAT(dato, '%d-%m-%Y') AS tid,
tilfoejelser.*,
DATE_FORMAT(datotf, '%d-%m-%Y') AS tid2
FROM arbejdsopgave LEFT JOIN tilfoejelser
ON arbejdsopgave.sub_id=tilfoejelser.sub_id2
WHERE arbejdsopgave.cat_id = '$id'
AND datotf IS NULL
OR datotf = (select max(datotf)
FROM tilfoejelser
WHERE arbejdsopgave.sub_id=tilfoejelser.sub_id2)
ORDER BY arbejdsopgave.sub_id")
or die(mysql_error());
I need to to only show rows from the table "arbejdsopgave" where arbejdsopgave.cat_id = '$id' but also where arbejdsopgave.status = 'closed'
I tried both in the join on and in the where.
First I get everything despite the extra where. Secondly I get only the rows where there are a join.
Can anyone help me - I am amazed that i even made it this far looking at my Query... Pls help
This should work:
SELECT arbejdsopgave.*, DATE_FORMAT(dato, '%d-%m-%Y') AS tid, tilfoejelser.*, DATE_FORMAT(datotf, '%d-%m-%Y') AS tid2
FROM arbejdsopgave
LEFT JOIN tilfoejelser ON arbejdsopgave.sub_id=tilfoejelser.sub_id2
WHERE arbejdsopgave.cat_id = '$id'
AND arbejdsopgave.status = 'closed'
AND (datotf IS NULL
OR datotf = (
select max(datotf) FROM tilfoejelser
WHERE arbejdsopgave.sub_id=tilfoejelser.sub_id2) )
ORDER BY arbejdsopgave.sub_id
I suppose you forget about () around datotf OR condition
I have three tables
1.course(c_id(pk),c_name,sem_no);
2.student(s_id(pk),s_name,user_name,password);
3.student_info(s_id(fk),c_id(fk));
I have logged in the student, THEN i run this query
SELECT distinct sem_no FROM course,student_info WHERE course.c_id=student_info.c_id and s_id='0001' ORDER BY sem_no ;
It shows all the semester he passed including the running semester..
now
I want to show the last value of sem_no column as his current semester..
how can I grab the last value of the sem_no?
Any help will be appreciated!
Here is an improved version of what you want:
SELECT sem_no
FROM course c join
student_info si
on c.c_id = si.c_id and s_id = '0001'
ORDER BY sem_no desc
LIMIT 1;
Note the use of proper ANSI join syntax. Also notice the use of table aliases, which make the query easier to read.
Try this:
SELECT sem_no
FROM course as t1 join
student_info as t2
on t1.c_id = t2.c_id
WHERE t2.s_id = '0001'
ORDER BY sem_no desc
LIMIT 1;
When selecting from mysql, you can order by either ascending or descending. ascending would be from 1-10 eg. and descending would be 10-1. So
SELECT distinct sem_no FROM course.student_info
WHERE course.c_id=student_info.c_id and s_id='0001' ORDER BY sem_no desc LIMIT 1;
would return last row, and only last row.
can you help me
$sql="select * from table1 where id='1,2,3,4'";
{
$sql2="select distinct column1 from table2 where column2='".$row['id']."' and left(date,10) BETWEEN '".$date_from."' AND '".$date_to."'";
}
I need to sort $sql by number of rows descending for $sql2 by id
If I understand you correctly, you want the results from $sql ordered by number of rows found by $sql2 for each row in $sql. This join should do that, it joins table2 and orders by the count, descending.
SELECT t1.id
FROM table1 t1
LEFT JOIN table2 t2
ON t1.id=t2.column2
WHERE id IN (1,2,3,4) -- should it really be = '1,2,3,4'?
AND LEFT(date,10) BETWEEN '2013-01-01' AND '2013-12-31'
GROUP BY t1.id
ORDER BY COUNT(DISTINCT t2.column1) DESC
An SQLfiddle to test with.
Where is the mistake?
Thank you!
SELECT Table.id,
Table.name,
Table.kommentar,
Table.pictureurl
WHERE Table.news_id = 5, //without this line the query works!
COUNT( Table1.comment_id ) AS numComments
FROM DATABASE.news_comments AS Table
LEFT JOIN DATABASE.news_comments_comments AS Table1
ON Table1.comment_id = Table.id
GROUP BY Table.id
ORDER BY Table.id DESC LIMIT 0,50
WHERE clause should be after the FROM clause
SELECT `Table`.id,
`Table`.name,
`Table`.kommentar,
`Table`.pictureurl,
COUNT( Table1.comment_id ) AS numComments
FROM DATABASE.news_comments AS `Table`
LEFT JOIN DATABASE.news_comments_comments AS Table1
ON Table1.comment_id = `Table`.id
WHERE `Table`.news_id = 5 // <=== HERE
GROUP BY `Table`.id
ORDER BY `Table`.id DESC LIMIT 0,50
One more thing, your alias which is Table should be escape with backtick since it a Reserved Keyword in MySQL
SELECT Table.id,
Table.name,
Table.kommentar,
Table.pictureurl,
COUNT( Table1.comment_id ) AS numComments
FROM DATABASE.news_comments AS Table
LEFT JOIN DATABASE.news_comments_comments AS Table1
ON Table1.comment_id = Table.id
WHERE Table.news_id = 5 //===> where should be here
GROUP BY Table.id
ORDER BY Table.id DESC LIMIT 0,50
I'm currently working on a news database website and I can't seem to create a query to select the 5 hottest news articles. The 2 tables of the database that are affected for this query are:
News - containing all news items (id, author, message, etc.)
Rates - containing all ratings on news items (id, news_id, rating, etc.)
Now my query should select 5 news_ids from the table Rates with the highest average rating and most votes ( so: ordered by AVG(Rating) and COUNT(*) I supposed ). I first tried to make my query as well get all info of these news_ids from the News table instantly ( using a WHERE id IN(--the query selecting the 5 hottest news_ids--) clause ) but that returned an error of my MySql Version not being cappable of using LIMIT inside of the WHERE IN clause sub-query.
Well, I hope you can help me out on the first query that has to select those 5 news_ids. The query I got as for now ( but not fully working ) is:
SELECT news_id FROM
(SELECT news_id, AVG(rating) AS average_r, COUNT(*) AS amt_r
FROM rates
GROUP BY news_id
ORDER BY average_r,amt_r
DESC LIMIT 5
) AS news_rates
or in content with the rest of my script:
$get_hot_news_ids = mysql_query("SELECT news_id FROM
(SELECT news_id, AVG(rating) AS average_r, COUNT(*) AS amt_r
FROM rates
GROUP BY news_id
ORDER BY average_r,amt_r DESC LIMIT 5) AS news_rates");
$first = 1;
while($news_id = mysql_fetch_assoc($get_hot_news_ids)) {
if(!$first) {
$hot_news_ids .= " ,";
}else{
$first = 0;
}
$hot_news_ids .= $news_id['news_id'];
}
//print_r($hot_news_ids);
$get_hot_news = mysql_query("SELECT * FROM news
WHERE id IN($hot_news_ids)
ORDER BY FIELD(id, $hot_news_ids)");
Are you sure both average_r and amt_r are both in descending order?
SELECT news_id FROM
(SELECT news_id, AVG(rating) AS average_r, COUNT(*) AS amt_r
FROM rates
GROUP BY news_id
ORDER BY average_r DESC, amt_r DESC
LIMIT 5
) AS news_rates
Try this:
SELECT TOP 5 N.id, N.author, N.message, AVG(R.rating) AS rate, COUNT(R.news_id) AS votes
FROM news N
INNER JOIN rates R ON N.id = R.news_id
GROUP BY N.id, N.author, N.message
ORDER BY rate, votes
You can use a join instead, which will allow the limit:
SELECT *
FROM news n JOIN (
SELECT news_id, AVG(rating) AS average_r, COUNT(*) AS amt_r
FROM rates
GROUP BY news_id
ORDER BY average_r,amt_r DESC
LIMIT 5
) top5 ON n.news_id = top5.news_id
ORDER BY top5.average_r,top5.amt_r
Note: You might want to change your query to a ORDER BY average_r DESC, amt_r DESC to get the highest rated items, instead of the lowest rated.