Pull From Two MySQL Tables [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 two MySQL tables with the following...
Apps
ID | Name
1 App1
2 App2
and...
Minutes
ID | AppID | Amount
1 1 10
2 1 15
3 2 35
I want to pull the Apps and display them by time used. Should look like this...
App2 35 minutes
App1 25 minutes
How can I do this?
Thank you.

You can use a JOIN with a GROUP BY. To add the amounts together use the SUM aggregate funcion.
SELECT name, SUM(amount)
FROM `apps`
JOIN minutes ON `apps`.`id` = `minutes`.`appid`
GROUP BY `apps`.`id`

Use a JOIN (SQL):
SELECT
Apps.Name,
Minutes.AppID,
SUM(Minutes.Amount)
FROM Minutes
INNER JOIN Apps ON Apps.ID = Minutes.AppID
GROUP BY Apps.Name, Minutes.AppID

Related

Mysql team/score [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 create an query to have the all matchs that a player win.
So, I have this table
ID_PLAYER1 ID_PLAYER2 SCORE_PLAYER1 SCORE_PLAYER2
1 4 5 3
2 1 3 4
3 4 2 0
I want to have this :
ID_PLAYER MATCH_WIN
1 2
2 0
3 1
4 0
Thank you !
I'm answering this because it's non-trivial and because I want to see if I can actually do it :p
SELECT `ID_PLAYER`, COUNT(*) AS `MATCH_WIN` FROM
(SELECT
IF(`SCORE_PLAYER1`>`SCORE_PLAYER2`, `ID_PLAYER1`, `ID_PLAYER2`) AS `ID_PLAYER`
FROM `your_table_name_here`
) AS `tmp`
GROUP BY `ID_PLAYER`

Getting rooms that are free between two dates 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 have two tables in MySQL. First is a Room.
and second is Reservation.
In Table reservation i have Room_Id and Reservation_DateFrom and Reservation_DateTo
How can i make a query that take two random dates for example (12-12-2012 and 21-12-2012) and see if the specific room is free all of the days from 12-12-2012 to 21-12-2012 if i have
DateFrom and DateTo for rooms in Reservation. I want to list all rooms that are available for rent on these days. For example if Room 1 has reservation from 15-12-2012 to 20-12-2012
it will not return it because some days from 12-12-2012 to 21-12-2012 are already reserved.
Thanks.
SELECT Ro.*
FROM Room Ro
WHERE NOT EXISTS (SELECT 1 FROM Reservation Re
WHERE Ro.Id = Re.Room_Id
AND ([yourStartDate] BETWEEN Re.Reservation_DateFrom AND Re.Reservation_DateTo
OR [yourEndDate] BETWEEN Re.Reservation_DateFrom AND Re.Reservation_DateTo)
)
it'll grab rooms that don't have any reservations that collide with your date range.

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 to retrive the values from my sql table [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 (stratageeks,HCL,IMIMobile) value from the table where user_id=wordpress how it possible
This is my Table
id user_id company_name number_save
---------------------------------------------------------------
1 wordpress Stratageeks 1
2 Manju Stratageeks 1
3 Manju Wipro 2
4 wordpress HCL 2
5 wordpress IMIMobiles 3
SELECT company_name FROM table WHERE user_id='wordpress'
You should consider learning basic SQL if you plan on doing this in the future.
If you want distinct results, use:
SELECT distinct company_name FROM table WHERE user_id='wordpress'

Count from 2 fields [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
As MySQL/PHP Noob I am struggeling to solve the following:
Table called "cars"
Fields called "modell" and "engine"
|Modell |engine
=====================
|Volvo |2,0
|Volvo |2,6
|Volvo |2,0
|Saab |1,8
|Saab |2,1
|Saab |1,8
|Saab |1,8
|Chevrolet |4,2
Would like to write a question and get as answer:
Here are:
2 volvo with 2,0 engine
1 volvo with 2,6 engine
3 saab with 1,8 engine
1 saab with 2,1 engine
1 Chevrolet with 4,2 engine
SELECT count(*) AS count,
Modell,
engine
FROM cars
GROUP BY Modell,engine
select modell, engine, count(*) as cnt
from cars
group by modell, engine
SELECT COUNT(*),Modell,ENGINE FROM cars GROUP BY modell,ENGINE

Categories