How can i select last 10 charge code that user bought? [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a table in mysql that has three column , one is the code that user bought it, the others is the time that this code has bought and last one is the buyers phone number; for exampale :
#CODE #phone #date
2154856265 3214490 2013-08-15
so now how can i make a query in php format that return me this statement :
i want the last 10 code that the user = 3214490 ordered by the date?
thanks for your patient ;)

You're kidding, right? This is about as trivial a query as they get.
select code
from table
where phone = 3214490
order by date desc
limit 10

You can try something like this:-
SELECT * FROM (
SELECT * FROM table where user = 3214490 ORDER BY date DESC LIMIT 10
) sub
ORDER BY date ASC

Assuming you're using 'phone' for the user id, you want a query like this:
SELECT * FROM [table_name] WHERE `phone` = 3214490 ORDER BY `date` DESC LIMIT 10
Replace [table_name] with the name of the table these records are stored in, as you didn't specify it in your question.

select c.code from directory c where c.phone= '3214490' order by c.date limit 0,10
This might help

Related

How to select different columns sum in mysql [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to select the sum of different cites according to time column in my database. The required result will be as like below image.
Please have a look at the image.
AND finally I want to print the above data in my web page using PHP.
you can use a combination of SUM(CASE...) and GROUP BY clause. Something like this
SELECT
city,
SUM(CASE WHEN MONTH(orderDate)=1 THEN amount ELSE 0 END) as january,
SUM(CASE WHEN MONTH(orderDate)=2 THEN amount ELSE 0 END) as february,
SUM(CASE WHEN MONTH(orderDate)=3 THEN amount ELSE 0 END) as march
FROM orders
GROUP BY city
I'm assuming the table name is ORDERS and using the column orderDate to identify the mounth.
Good luck, bro.
If I understand you write, may be something like:
SELECT city, sum(amount)
FROM table_name
WHERE MONTH(date_column)=12
GROUP BY city;

mysql group by only date and count how many results [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a sample table name (info) in mysql database as follows:
id dateAdded
----------------------------
1 2013-12-24 03:03:19
2 2013-12-24 03:04:19
3 2013-12-24 03:06:14
4 2013-12-24 03:07:23
5 2013-12-25 03:04:19
6 2013-12-26 03:02:19
7 2013-12-26 03:03:19
I have another table name (error) as follows:
id date
----------------------------
11 2013-12-24 03:03:19
22 2013-12-24 03:04:19
33 2013-12-25 03:06:14
53 2013-12-25 03:04:19
62 2013-12-26 03:02:19
I want to COUNT how many ids from the two tables (info and error) with the same dates , so the result out will be :
date countinfo counterror
----------------------------------------
2013-12-24 4 2
2013-12-25 1 2
2013-12-26 2 1
Please if you could help me out with the Mysql query
Give this a try, maybe it can be written in a better way but this gives the desired output.
SELECT
DATE(Sub.dateadded) as `Date`,
( SELECT count(id)
FROM test.info
WHERE DATE(info.dateadded)=DATE(Sub.dateAdded)) as `CountInfo`,
( SELECT count(id)
FROM test.`error`
WHERE DATE(`error`.`date`)=DATE(Sub.dateAdded)) as `ErrorInfo`
FROM
(
SELECT `date` as dateadded
from test.`error`
UNION SELECT dateadded
FROM test.info
) Sub
GROUP BY Date(Sub.dateadded)
Notice that my database name used here is test, change that to your database name.
Since the date field is from different tables, you must UNION them in a subquery so that you can get the relevant dates. Then from there a simple subquery in the select is executed with the dateparameter.
In the future, try to name your tables with names that is not a datatype or function name etc, then the ` is not needed to wrap the database,table,column names
EDIT
If you want specific dates, just make use of WHERE.
Add this line before the GROUP BY
WHERE DATE(Sub.dateadded) IN ('2013-12-24','2013-12-25')
If you want between a time span you can do this
WHERE DATE(Sub.dateadded) BETWEEN '2013-12-24' AND '2013-12-30'
This will give the dates available between 24-30 of December.
Try this, please rename the table name that I have provided.
Info : Join both table get the count of distinct Id
select Date(dateadded) as datepart,
count(distinct(infotable.id)) as countInfo,
count(distinct(errortable.id)) as counterror from infotable inner join errortable
on Date(infotable.dateadded)=Date(errortable.date)
group by datepart
Try this for a pure MySQL approach:
SELECT dateAdded, COUNT(*) as count_info FROM info GROUP BY dateAdded;
SELECT date, COUNT(*) as count_error FROM error GROUP BY date;

How can I Use the while loop until i get 3 rows from a table in mySQL? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to retrieve content content based on their time stamp. And my query is
SELECT link,title,timestamp,photo,link,author,comments FROM posts
WHERE timestamp='*Todays Date -1*' LIMIT 6
If the query returns zero rows, how can I query for results 2 days back or three days back using a while statement?
Why not just query full table (without timestamp filter) and return limit of 6 rows. in this case there would be no reason to iteratatively query
SELECT link,title,`timestamp`,photo,link,author,comments
FROM posts
ORDER BY `timestamp` DESC
LIMIT 6
This gives you up to 6 records with most recent records returned first. You should have index on timestamp field for this query, which is OK because you should already have one because you were previously trying to filter on this field.
If you have a case where more than 3 rows show up within last day and you don't need to show records from previous days, this is easy enough to achieve by inspecting the values as you loop through the result set.
This also prevents you from getting into an infinite loop if you only have 2 records in the database.
Also note timestamp is a reserved word, you should be careful when using such field names. If you have to use a reserved word as field name, you must enclose it with backticks.
maybe a for could work
$i=1
for ($row='';$count=0;$i++) {
$result = mysql_query("SELECT link,title,timestamp,photo,link,author,comments FROM posts WHERE timestamp='*Todays Date -$i *' LIMIT 6");
$count = mysql_num_rows($result);
}
I think the query that you want both filters the results and orders them.
SELECT link, title, timestamp, photo, link, author, comments
FROM posts
WHERE timestamp <= '*Todays Date -1*'
ORDER BY timestamp desc
LIMIT 6;
If you actually want 3 rows and not 6 (as suggested by the post title), then change the 6 to 3.

MySQL query to display one record before today's date [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I can't find an answer to my question, it's probably easy but... anyway!
I have a database with every NHL game for one specific team in a table. Every game has been entered in the good order.
On my home page, I would like to display the upcoming game as well as the result of the previous game. How can I create a mySQL query to display the previous game based on today's date?
Thanks!
Do your game records have a timestamp or datetime value?
If so you could write a query ordering your games by the date smaller that now() and limit by one.
The query should look like this:
select * from games where gamedate < now() order by gamedate desc limit 1

How can i fetch and compare time of related post from database? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am using mysql as database and saved time as time stamp of related posts in table, now i want to fetch the post made rrecently . How can i compare and fetch the post made recently?
you can fetch rows in descending order of your column (time_stamp).
ex:
SELECT * FROM Table_Name ORDER BY col_time_stamp DESC;
col_time_stamp : is the column
select top 1 * from posts order by timestamp desc
try this
SELECT * FROM Table_Name ORDER BY col_time_stamp DESC LIMIT 0;

Categories