MySQL Min(), MAX() using the IDs from another table - mysql

I have two very simple MySQL tables: Table1 with a list of unique item_ids and the second one Table2 with records using the the item ids form the first table. Table2 contains also the date the record was made. To recap here is how the tables look like:
Table1
columns: item_id <---these are unique IDs
Table2
columns: item_id, item_title, time_stamp
What I need is a MySQL query that will give me a list with the date of the first and last record have been made for each item_id in Table2. It's imperative that Table2 will look just for the item_id in Table1 because Table2 contains item_ids that are not contained in Table1. So in other words I'm using Table1 just for sorting based on item_id.
Your help is greatly appreciated.

SELECT t2.item_id, MIN(t2.time_stamp), MAX(t2.time_stamp)
FROM table2 t2
JOIN table1 t1
ON t1.item_id = t2.item_id
GROUP BY
t1.item_id
ORDER BY
t1.ordering_column
In MySQL, you can use ungrouped and unaggregated expressions in SELECT and ORDER BY.

select item_id, maxts.ts, mints.ts
from (select item_id, max(time_stamp) as ts from Table2 group by item_id) maxts
join (select item_id, min(time_stamp) from Table2 group by item_id) mints on maxts.item_id=mints.item_id
join Table1.item_id t1 on t1.item_id=mints.item_id;

Related

Easy query on 2 tables in SQL

I have a table, TABLE1, with two columns: SSN and Date.
I have another table, TABLE2, with SSN, name and Surname.
I want to view the Name and the Surname of the person who is associated to the SSN that appears the highest number of times in the first table (the one with SSN and Date)
Which query should I write?
The fastest way is probably to aggregation before joining:
select *
from (select t1.ssn
from table1 t1
group by t1.ssn
order by count(*) desc
limit 1
) t1 left join
table2 t2
using (ssn)
Try this.
SELECT t1.ssn, t2.name, t2.surname, count(*) as vol
FROM table1 t1
LEFT JOIN table2 t1 on t1.ssn = t2.ssn
GROUP BY 1
ORDER BY count(*) DESC
LIMIT 1
Use a subquery in the WHERE clause which returns the SSN that appears the most in table1:
SELECT *
FROM table2
WHERE SSN = (SELECT SSN FROM table1 GROUP BY SSN ORDER BY COUNT(*) DESC LIMIT 1)
With an index on SSN (which I assume exists) this is the fastest way to get the result.

How to group only by joined table column with Doctrine?

I have one join table:
table1_id|table2_id
1|1
1|2
2|1
2|2
3|1
I need to select rows grouped by table2. But I need special grouping. In result row I want to see the following:
table1.id|grouped
1,2|1,2
3|1
I tried different ways.
Select from table1 and group by table2.id. Fail - MySql required query to be grouped my primary table as well.
Select from table2 and group by table1.id and table2.id . Fail - it selects rows from table2 separately, I want them to be concatenated as I shown in the example.
Select from table1 and group by group_concat(table2.id). But MySql does not allow to group by aggregated functions.
My example query: http://sqlfiddle.com/#!9/934b64/4. I need almost the same, but so that it will one row with values:
1,2|1,2
3|1
You are almost there.
You need to use GROUP_CONCAT in SELECT clause, not in GROUP BY clause.
SELECT table1_id,GROUP_CONCAT(table2_id) as grouped
FROM table
GROUP BY table1_id;
See the result in SQL Fiddle
For your requirement, please try the below query:
select t1.id,t1.value,
group_concat(t2.id) as grouped_ids,
group_concat(t2.value) as grouped_values
from table1 t1
join joined_table jt on jt.id1 = t1.id
join table2 t2 on t2.id = jt.id2
group by t1.id,t1.value
Result in SQL Fiddle

MySQL- Compare Customer IDs from two tables to determine who made no purchases

I have two tables in SQL, one with information on customers and orders placed by them (columns include customerid, contactname, orderid, quantity, to name a few). My second table is simply a list of all customer ids, and my task is to determine which customerID didn't make a purchase. Some of the customer ids made multiple purchases, so I am unsure of how to use SELECT DISTINCT to compare the two tables.
join the second table and filter the results
SELECT DISTINCT t1.customerid, t1.contactname
FROM table1 t1
JOIN table2 t2
ON t1.customerid = t2.customerid
WHERE t1.customerid = t2.customerid
Use not exists:
select t2.customerid
from table2 t2
where not exists (select 1 from table1 t1 where t1.customerid = t2.customerid);

Sorting results based on key from different table

In table1 one of the fields is member_id.
In table2 one of the fields is member_username and the id field in this table is equal to the member_id field in table2.
The goal is to display all results from table1 in ascending alphabetical order based on member_username from table2.
Basically I need to resolve the member_id from table1 to a member_username from table2 and sort them alphabetically.
Any ideas?
You need to use a join from table1 to table2 to pick up the username, then sort on this field. You just need to be wary of a one-to-many relationship, i.e. if a member might have more than one entry in table2 with the same id you may get more rows than you expect.
select *
from table1
left join table2 on table2.id = table1.member_id
order by table2.member_username
If I didn't misunderstand your question, try joining both tables together and sort by member_username.
SELECT t1.*,
t2.member_username
FROM table1 AS t1
INNER JOIN table2 AS t2 ON t1.member_id = t2.id
ORDER BY t2.member_username ASC;
You can leave t2.member_username in the SELECT-part of the query, I just put it there for reference.

sql left join with distinct correct?

I'm trying to do a join between tables 1 and 2 which have a 1 to many relationship.
table1 has the following fields
createdate
contact
tkey (surrogate key)
table2 has the following fields
tkey (primary key)
status
userfld1
description
I want to show all items in table2 with their corresponding items in table1 grouped by table2.userfld1
select distinct t2.userfld1, t2.status, t2.description, t1.createdate, t1.contact
from table2 as t2 left join table1 as t1
on t2.tkey = t1.tkey
group by t2.userfld1
is this correct?
No that's not correct, you can't select columns that aren't in the group by unless they are contained in an aggregate function. And I think what you are asking for doesn't even make sense. My best guess is that you mean ORDER BY, not GROUP BY:
SELECT DISTINCT t2.userfld1, t2.status, t2.description, t1.createdate, t1.contact
FROM table2 t2
LEFT JOIN table1 t1
ON t2.tkey = t1.tkey
ORDER BY t2.userfld1
Three other errors that I've fixed:
SELECT ... FROM not SELECT ... WHERE
You should join with a table, not a column.
You had no aliases after the table names, but later refer to these missing aliases.
I think what you're looking for is order by, not group by, and I also fixed your query:
select t2.userfld1, t2.status, t2.description, t1.createdate, t1.contact
where table2 t2 left join table1 t1
on t2.tkey = t1.tkey
order by t2.userfld1
Is this what you were looking for?