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.
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 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
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 8 years ago.
Improve this question
I'm trying to create a database that holds all my information with questions, users, answers etc.
I've been thinking all day how to solve this problem of mine. Okey, so the problem is like this:
Let's say I have a table in my DB, with questions (300++) and the user that logs on will get a random question shown. But never the same question again, so I'll need to store this information in a separate table with the user-ID and the question-ID. And I'll need to create another table that stores the answers to the questions.
So how would this PHP/MYSQL-code look like? Because I'll need to find a random question that hasnt been shown to the same user again.
If something is unclear, please let me know. And thanks in advance
You can use RAND() function and NOT IN
Example:
SELECT * FROM `questions` WHERE id NOT IN (5, 3) ORDER BY RAND() LIMIT 1;
You have to fetch question id using sub query
Example
SELECT * FROM `questions` WHERE id NOT IN (SELECT question_id FROM shown_questions WHERE user_id=1) ORDER BY RAND() LIMIT 1;
You can use an outer join to select items that have not been used already. It might be a bit faster than NOT IN if you set up the indexes:
SELECT questions.* FROM questions
LEFT JOIN shown_questions
ON (questions.id=show_questions.question AND user_id=42)
WHERE shown_questions.question IS NULL
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 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.