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
Related
The table1 has only index column and the table2 is just to contain versions.
I want to select every index and its latest version. (the yellow cells)
The table2 has datetime column to know which one is latest.
When table1.idx = table2.belongTo, how to combine tables with a single query?
Edit; Corrected the answer to return all columns from table2.
You first have to derive the second table by selecting max(datetime) and then join based on the condition you gave.
Here the second table t2 will create row_number based on max(datetime) value for each BelongTo. Thanks to this answer for it.
select t1.*,t2.idx,t2.belongTo,t2.datetime1
from table1 t1
inner join
(
select t11.idx,t11.belongTo,t11.datetime1,count(*) as row_number from
table2 t11
inner join table2 t12
on t11.belongTo=t12.belongTo
and t11.datetime1 <= t12.datetime1
group by t11.belongTo,t11.datetime1
) t2 /*this table will create row_number based on max datetime value for each belongTo*/
on t1.idx=t2.belongTo
where t2.row_number=1
See SQL Fiddle demo here
http://sqlfiddle.com/#!9/e5ada/10
I have 2 tables which are linked by column named MID.
I want to fetch name from 1st table but the sequence is mentioned in 2nd table.
My query is as follows:
select name from table1 where MID in(select MID from table2 where CID="100" ORDER BY sequenceNum);
If i only run the query mentioned inside brackets then i get the data ordered by sequence.
But the above query is fetching the data from db as it is and not arranging it in sequence. What can be the problem?
I think this shoukld do the trick...
SELECT name FROM table1
INNER JOIN table2 ON Table2.MID = table1.MID AND CID="100"
ORDER BY
table2.sequenceNum
You want merge two tables and order results by merged table:
SELECT table1.name
FROM table1
LEFT JOIN table2 ON (table2.MID = table1.MID)
WHERE table2.CID = "100"
ORDER BY table2.sequenceNum;
or
SELECT table1.name
FROM table1
LEFT JOIN table2 ON (table2.MID = table1.MID AND table2.CID = "100")
ORDER BY table2.sequenceNum;
If you want get field from concrete table, use table prefix like table1.name
I have 2 tables in the same database.
I want to merge them based on the common id column. Because the tables are too huge I am not sure if there are duplicates.
How is it possible to merge these two tables into one based on the id and be sure that there are no duplicates?
SELECT *
FROM table1,table2
JOIN
GROUP BY id
What do you mean by merging two tables? Do you want records and columns from both the tables or columns from one and records from both?
Either way you will need to change the join clause only.
You could do a join on the columns you wish to
SELECT DISTINCT *
FROM table1 tb1
JOIN table2 tb2
ON table1.id = table2.id
Now if you want columns from only table1 do a LEFT JOIN
If you want columns from only table2 then a RIGHT JOIN
If you want columns from both the tables, use the query as is.
DISTINCT ensures that you get only a single row if there are multiple rows with the same data (but this distinct will check values for all columns in a row whether they are different or the same)
Union won't help if both tables have different number of columns. If you don't know about joins then use a Cartesian product
select distinct *
from table1 tb1, table2 tb2
where tb1.id = tb2.id
Where id is the column that is common between the tables.
Here if you want columns from only table1 do
select distinct tb1.*
Similarly replace tb1 by tb2 in the above statement if you just want table2 columns.
select distinct tb2.*
If you want cols from both just write '*'
In either cases I.e. joins and products said above if you need selective columns just write a table alias. E.g.
Consider :
table1 has id, foo, bar as columns
table2 has id, name,roll no, age
you want only id, foo, name from both the tables in the select query result
do this:
select distinct tb1.id, tb1.foo, tb2.name
from table1 tb1
join table2 tb2
on tb1.id=tb2.id
Same goes for the Cartesian product query. tb1, tb2 are BTW called as a table aliases.
If you want data from both the tables even if they have nothing in common just do
select distinct *
from table1 , table2
Note that this cannot be achieved using a join as join requires a common column to join 'on'
I am not sure What exactly do you want but anyway, this is your code
SELECT *
FROM table1,table2
JOIN
GROUP BY id
i just edit your query
SELECT *
FROM table1 JOIN table2
on table2.id = table1.id
GROUP BY table1.id // here you have to add table
//on which you will be group by at this moment this is table1
Try UNION:
https://dev.mysql.com/doc/refman/5.0/en/union.html
IT is very simple. Hope it will help.
Also you should have a look at "DISTINCT".
Trying to list all what's in table 1 and records under it in table 2
Table one each row has an id , and each row in table 2 has idontable1
select table1.*, count(table2.idintable1)as total
from table1
left join table2 on table1.id=table2.idintable1
WHERE table1.deleted='0' AND table2.deleted=0
group by
table2.idintable1
My current problem is rows from table1 with 0 records in table2 are not displayed
I want them to be displayed
The query that you want is:
select t1.*, count(t2.idintable1) as total
from table1 t1 left join
table2 t2
on t1.id = t1.idintable1 and t2.deleted = 0
where t1.deleted = 0
group by t1.id;
Here are the changes:
The condition on t2.deleted was moved to the on clause. Otherwise, this turns the outer join into an inner join.
The condition on t1.deleted remains in the where clause, because presumably you really do want this as a filter condition.
The group by clause is based on t1.id, because t2.idintable1 will be NULL when there are no matches. Just using t1.id is fine, assuming that id is unique (or a primary key) in table1.
The table aliases are not strictly necessary, but they make queries easier to write and to read.
You should GROUP BY table1.id.
The LEFT JOIN ensures all the rows from table1 appear in the result set. Those that do not have a pair in table2 will appear with NULL in field table2.idintable1. Because of that your original GROUP BY clause produces a single row for all the rows from table1 that do not appear in table2 (instead of one row for each row of table1).
You have fallen into mysql's non-standard group by support trap.
Change your group by to list all columns of table 1:
group by table1.id, table1.name, etc
or list the column positions of all table1 columns in the select:
group by 1, 2, 3, 4, etc
Or use a subquery to get the count vs the id, and join table1 to that.
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.