Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have following in php code:
$result=mysql_query ("SELECT title, image FROM board order by date limit 15");cq();
I would like to replace 15 with result from sql query:
SELECT count FROM cat_count
So I put to php but it doesn't work:
$cat_count=mysql_query ("SELECT count FROM cat_count");cq();
$result=mysql_query ("SELECT title, image FROM board order by date limit ".$cat_count."");cq();
Thanks
You can AS alias to get count in a variable and then use it in your next query.
$result=mysql_query("SELECT count(*) as total from cat_count");
$data=mysql_fetch_assoc($result);
echo $data['total'];
Then use it as:
$result=mysql_query ("SELECT title, image FROM board order by date limit ".$data['total']);cq();
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to fetch random record from the table but repeated game id record is will become out only once at a time. I am sending a screenshot of my table
You need to use MySQL RAND() function.
Query:
SELECT * FROM lessons_game ORDER BY RAND() LIMIT 1
// Put proper fields instead of *
Explanation:
ORDER BY RAND() will shuffle records random records from DB Table.
And we will get random records after we execute the query result set.
We are putting LIMIT 1 so only one record will be pulled out.
Try the following command
SELECT * FROM lessons_game ORDER BY RAND() LIMIT 1
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have 2 queries. I want to get MAX date. If I get MAX date my subquery will look like below.
Query 1:
select
tb_format.format
from
tb_format
where
tb_format.id not in
(
select
penyakit.format
from
penyakit
where
penyakit.id_puskesmas =$ id
AND MONTH(penyakit.waktu_upload) = MONTH(CURRENT_DATE)
)
I want to add MAX on this MONTH(penyakit.waktu_upload).
Query 2:
select
tb_format.format
from
tb_format
where
tb_format.id not in
(
select
penyakit.format
from
penyakit
where
penyakit.id_puskesmas =$ id
AND MAX(MONTH(penyakit.waktu_upload)) = MONTH(CURRENT_DATE) -- HERE
)
However, I'm getting an error:
You can't have MAX in a WHERE clause, you can have it in a HAVING clause (this explains the difference a bit more)...
select
penyakit.format
from
penyakit
where
penyakit.id_puskesmas =$ id
group by
penyakit.format
having
MAX(MONTH(penyakit.waktu_upload)) = MONTH(CURRENT_DATE)
There has to be a GROUP BY clause and this gives the basis for what the MAX operates over, the HAVING clause is almost a WHERE clause for the GROUP BY.
Not sure what the $ id is so I've just copied from your original code, but this will need to be corrected.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to join this query with another, but that will require doing everything at once without leaving this statement.
MYSQL
"SELECT pid FROM posts WHERE uid = ? && state = 'a' ORDER BY time DESC LIMIT 5"
Using PHP's while.
while($row = $get->fetch()){
echo $row['pid'].' ,';
}
I get something like : 2,5,8,31,40
Is there a way to get the same values without getting out of the query?
Something like SELECT as returned_values
So I could do : $row['returned_values']; and get : "2,5,8,31,40"
Just use GROUP_CONCAT :
"select GROUP_CONCAT(pid) as returned_values from ( SELECT pid FROM posts WHERE
uid = ?
&& state = 'a'
ORDER BY time DESC
LIMIT 5) as s"
Refer this : http://www.mysqltutorial.org/mysql-group_concat/
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Here's the link what I mean -
i.stack.imgur.com/yYQbu.png.
Query from 2 columns name and version.(may only with one column I'll put version in name column).
So , Wheres matches name from title or post from same column name and returns all lowest number from version(column).
Can anyone give me example of some query?
So far I have this query:
$name = substr($row[2],0,10);
$q = mysql_query("SELECT name from films WHERE name LIKE '$name'") or die(mysql_error());
I'm assuming your table has an id column as the primary key with auto increment.
In which case, you need to add an ORDER BY and LIMIT to your query.
$q = mysql_query("SELECT name from films WHERE name LIKE '$name' ORDER BY id DESC LIMIT 10") or die(mysql_error());
ORDER BY id DESC will make your query put the latest records first.
LIMIT 10 will ensure that you only grab 10 records.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I just wanna show last 10 or 30 depends on list if there is big data, searched over the net but all by correcting I can see only the 1st data not newest one.
$sql = "SELECT id, tag, count FROM url ORDER BY id, tag, count ASC LIMIT 1";
I am testing with two datat on database and want to display last one but all the time it shows the oldest one (first data), I changed ASC and DESC also, both time result is same.
Try this..
Order by id using "DESC" it get last to first and limit 0,10 means it display last 10 record
$sql = "SELECT id, tag, count FROM url ORDER BY id DESC LIMIT 0 10"
Use this one:
$Query = "SELECT id, tag, count FROM url ORDER BY id DESC LIMIT 10"
Change your query & try this:
$Query = "SELECT id, tag, count FROM url ORDER BY id DESC LIMIT 10";