Maybe this question has been answered somewhere else but I didn't know how can I apply that query in my working project and I am a beginner(new) to using MySQL.
I have 2 tables in my Phpmyadmin.
table-1 looks like this :
and table-2 looks like this
By using the below query I am able to join both tables together but in different column names.
SELECT DISTINCT `a`.`subject`,`b`.`subject`, `a`.`date`, `a`.`start_time`, `a`.`end_time`, `b`.`date`, `b`.`start_time`, `b`.`end_time`
from `time-table` as a
inner join `oral_time-table` as b on a.oral_token=b.token and a.class='EIGHTH' and a.medium='English' and a.faculty='GENERAL' and a.exam='Half-Yearly'
The below query results in this :
I want both data date name column in ascending order in both the tables. Therefore I need to join both the tables one by one.
With only one common column name both the table rows are joined. How can I achieve this and thanks in advance...
Desired Result :
date column ascending order.
You can achieve the desired result using UNION.
SELECT subject, `date`, start_time,
end_time
FROM TABLE-1
UNION
SELECT subject, `date`, start_time,
end_time
FROM TABLE-2
ORDER BY `date` ASC
Don't use date as any column name as it is a reserved keyword and is
not a good practice.
Related
My database is called: (training_session)
I try to print out some information from my data, but I do not want to have any duplicates. I do get it somehow, may someone tell me what I do wrong?
SELECT DISTINCT athlete_id AND duration FROM training_session
SELECT DISTINCT athlete_id, duration FROM training_session
It works perfectly if i use only one column, but when I add another. it does not work.
I think you misunderstood the use of DISTINCT.
There is big difference between using DISTINCT and GROUP BY.
Both have some sort of goal, but they have different purpose.
You use DISTINCT if you want to show a series of columns and never repeat. That means you dont care about calculations or group function aggregates. DISTINCT will show different RESULTS if you keep adding more columns in your SELECT (if the table has many columns)
You use GROUP BY if you want to show "distinctively" on a certain selected columns and you use group function to calculate the data related to it. Therefore you use GROUP BY if you want to use group functions.
Please check group functions you can use in this link.
https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html
EDIT 1:
It seems like you are trying to get the "latest" of a certain athlete, I'll assume the current scenario if there is no ID.
Here is my alternate solution:
SELECT a.athlete_id ,
( SELECT b.duration
FROM training_session as b
WHERE b.athlete_id = a.athlete_id -- connect
ORDER BY [latest column to sort] DESC
LIMIT 1
) last_duration
FROM training_session as a
GROUP BY a.athlete_id
ORDER BY a.athlete_id
This syntax is called IN-SELECT subquery. With the help of LIMIT 1, it shows the topmost record. In-select subquery must have 1 record to return or else it shows error.
MySQL's DISTINCT clause is used to filter out duplicate recordsets.
If your query was SELECT DISTINCT athlete_id FROM training_session then your output would be:
athlete_id
----------
1
2
3
4
5
6
As soon as you add another column to your query (in your example, the column called duration) then each record resulting from your query are unique, hence the results you're getting. In other words the query is working correctly.
I have been trying but it seems I am missing something. I want to combine two results from two tables by a common field.
I would like to group results from these two queries by customer field.
SELECT errors.customer, count(errors.customer) as err_count,severity from errors group by customer,severity;
SELECT customer,sum(size) as Tot_size,count(customer) as Policy_count from backup group by customer;
I have tried this.
SELECT errors.customer, count(errors.customer) as err_count,severity from errors group by customer,severity union all SELECT customer,count(customer) as Policy_count ,sum(size) as Tot_size from backup group by customer;
But for some reason some columns are missing.
You should follow the requirements for union:
The UNION operator is used to combine the result-set of two or more SELECT statements.
Each SELECT statement within UNION must have the same number of columns
The columns must also have similar data types
The columns in each SELECT statement must also be in the same order
Apparently, the above items are not satisfied in your query.
Try something like this:
SELECT q1.customer, Tot_size, Policy_count, err_count, severity
FROM ( SELECT customer, SUM(size) AS Tot_size, COUNT(customer) AS Policy_count
FROM backup GROUP BY customer ) q1
LEFT JOIN ( SELECT customer, COUNT(customer) AS err_count, severity
FROM errors GROUP BY customer, severity ) q2 ON q1.costumer = q2.costumer
Your first query contains three columns and your second one contains two columns.
In order to use the UNION operator your two queries need to have the same amount of columns, and the columns should be compatible.
In your case the second query lacks a third column. If there is no corresponding column to use you can set a default such as
"'n/a' as severity "
if it should be textual or
"0 as severity "
for a numerical value.
Cheers Martin
I have a MySQL table :
when i count the entries of month January using the query :
SELECT COUNT(*) AS entries FROM daily_call_reports WHERE Month(datetime_in)='01' AND emp_id='E0001'
I got the result 5.
But i want to count same date rows as one.. in the table there are two row of same date 2016-01-21. Now how to count these two row as one..
try this
select count(*)
from
(SELECT distinct id,client_id,emp_id,...., CAST(re.datetime_in AS DATE) AS DATE_PURCHASED
FROM daily_call_reports re
WHERE Month(datetime_in)='01' AND emp_id='E0001')
In a table, a column may contain many duplicate values; and sometimes
you only want to list the different (distinct) values.
The DISTINCT keyword can be used to return only distinct (different)
values.
You have to use DISTINCT for counting the different rows.
SELECT COUNT(DISTINCT DATE_FORMAT(`datetime_in`, '%Y-%m-%d')) as entries FROM daily_call_reports WHERE month(datetime_in)='01' AND emp_id='E0001'
Try this one and let me know.
I have two tables with identical structures handling distinct data. I want to merge them, add a text field indicating where the data for that row came from, and order by a common field.
TABLE1
ID|NAME|YEAR
1,'peter',2008
2,'edward',2010
TABLE2
ID|NAME|YEAR
1,'compadre',2009
2,'vika',2011
DRAFT of query ( obviously is erroneous )
select * from TABLE1 JOIN TABLE2 order by YEAR asc
expected result:
1,'peter','iamfromTABLE1',2008
1,'compadre','iamfromTABLE2',2009
2,'edward','iamfromTABLE1',2010
2,'vika','iamfromTABLE2',2011
I know I can do this using PHP/MySQL, but is there not a more elegant way like the "One Simple Query".
Use a Union query and literals:
SELECT ID, Name, 'iamfromTABLE1' as indicator, Year
FROM Table1
UNION
SELECT ID, Name, 'iamfromTABLE2' as indicator, Year
FROM Table2
ORDER BY Year
EDIT: as indicator added on recommendation of iim.hlk
I have a table with 4 columns: name, date, version,and value. There's an composite index on all four, in that order. It has 20M rows: 2.000 names, approx 1.000 dates per name, approx 10 versions per date.
I'm trying to get a list that give for all names the highest date, the highest version on that date, and the associated value.
When I do
SELECT name,
MAX(date)
FROM table
GROUP BY name
I get good performance and the database uses the composite index
However, when I join the table to this in order to get the MAX(version) per name the query takes ages. There must be a way to get the result in about the same magnitude of time as the SELECT statement above? I can easily be done by using the index.
Try this: (I know it needs a few syntax tweaks for MySQL... ask for them and I will find them)
INSERT INTO #TempTable
SELECT name, MAX(Date) as Date
FROM table
Group By name
select table.name, table.date, max(table.version) as version
from table
inner join #TempTable on table.name = #temptable.name and table.date = #temptable.date
group by table.name, table.date