How to take the function max current-date on my query [closed] - php

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.

Related

how to get a unique record from select query in mysql each time [closed]

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

Select and sort data from one table on certain columns [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm looking to see if it is possible to select data from table A with a sort so the order of the data changes.
I then want to make a loop so it will go through each row and add it into table B.
Is this possible.
Use a variable #row_number to store the incremented num for your expected sorting.
SET #row_number = 0;
SELECT (#row_number:=#row_number + 1) AS num, Column1
FROM TableA
ORDER BY Column1
This result can be store in temporary table and based on that you can do loop/insert into the TableB

Query Execution [closed]

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 6 years ago.
Improve this question
How to execute this query in laravel. ?
select d1.update_id from ( select update_id, count(update_id)
as ct from updates_tags where tag_id in (67,33,86,55) group by
update_id) as d1 where d1.ct=4
you can use Raw Queries in Laravel
$results = DB::select(SELECT * FROM table WHERE id IN (1,2,3,4) AND );
http://fideloper.com/laravel-raw-queries
You can chain where conditions with eloquent, each chained one is evaluated like an and.
$update = Tag::whereIn('t_id',$tags)->whereIn('u_id',$tags)->where(/*more and conditions you could need*/)->lists('u_id')
Edit: If you need the ones coincident with u_id consider this:
$update = DB::table('tags')
->select('t_id', 'u_id', DB::raw('count(*) as total'))
->whereIn('t_id',$tags)
->where('total','=',count($tags))
->groupBy('u_id')
->lists('u_id');

Mysql Order by date multiple columns after joining two tables [closed]

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
Now i want like in 2015-08-04 the middle column of 2015-08-04 should be on same line order and 2015-08-05 should come down of both columns like if the both date matches they should be in same order like order by both columns its probably like ledger format my sql query
SELECT i.* , it.name AS type,td.qty AS issue, td.is_from_stock AS issue_qty_type,v.name AS vendor,o.name AS branch,td.date_from AS issue_date,l.name AS location,td.user AS user,td.remarks AS remarks, os.opening_quantity AS initial_stock,os.remaining_stock AS opening_quantity,os.opening_quantity_unit_price As opening_unit
FROM ". $this->tbl_inventories ." AS i
LEFT JOIN ". $this->tbl_inventorytypes ." AS it ON i.type_id = it.id
LEFT JOIN ". $this->tbl_distribution ." AS td ON td.item_id = i.item_id AND td.user_branch = i.user_branch
And i did like ORDER BY purchased_date,td.date_from
First of all, be bit specific on the image you have provided. I think its not possible to sort the data fetched from MySQL by the date directly as you have tried, since MySQL data output represents a single row for each set of data.
You have to breakdown to multiple query and put them in the respective arrays, so that you can filter out the data into final array i.e you have to rearange ta data into new array and display it to the table you have snapshot of.

assesment for php application code [closed]

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 8 years ago.
Improve this question
Thanks for reading.... still on my way to really getting the hang of php an MySQL queries.
I have a system that queries table entries... I have managed the joining of the 2 tables in my sql query.
Sample:
$sql = mysql_query("select * from table1,table2 where `field_table1` = `field_table2` and `x`= 'x' and y = 'y'")
My question on the validation for this query... The joined field in table1 has to be the same as field in table2. Is an if statement the solution and how would I approach this?
Basically I do not want the query to show the result of the search if field_table1 is not = to field_table2.
Thank you for your assistance.
SELECT *
FROM table1
INNER JOIN table2 ON...
WHERE table1.field != table2.field
That should pretty much take care of it.

Categories