How to put a column in the IN?
Example:
SELECT tb1.*,
(SELECT GROUP_CONCAT(name)
FROM tb2
WHERE id IN( tb1.ids_tb2 )) AS names
FROM tb1
Column tb1.ids_tb2 stores "1,2,3"
This is not working.
++++
I need 'ids_tb2' inside the IN
Example Tables http://sqlfiddle.com/#!9/08cfa9/1
Ids_tb2 = 1,3,4
Names != Fruit1,Fruit3,Fruit4
tks
Your table structure is not really ideal for this task. In database design you should always preserve the 'atomicity' of data, i. e. you should never work with lists of ids in any column. Instead use a link table lnk like this
| gid | fid |
|-----|-----|
| 1 | 1 |
| 1 | 3 |
| 1 | 4 |
| 2 | 1 |
| 2 | 5 |
Having such a structure you can then very easily do
SELECT grp , GROUP_CONCAT(name) fruits
FROM tb1
INNER JOIN lnk ON gid=grid
INNER JOIN tb2 ON frid=fid
GROUP BY grp
see here (demo).
The result will be
| grp | fruits |
|------|----------------------|
| Blue | Fruit1,Fruit3,Fruit4 |
| Red | Fruit1,Fruit5 |
Having said that - you could also do the following with your original database design (see here):
SELECT tb1.id,`group`,GROUP_CONCAT(name) fruits
FROM tb1
INNER JOIN tb2 ON FIND_IN_SET(tb2.id,ids_tb2)>0
GROUP BY `group`
Although I would not recommend doing this. Also you should not be using reserved SQL names for your columns like group, as you will always have to use back-ticks to mask them.
Revisiting ...
Actually, your initial approach does also work, you just have to replace your IN (..) clause by a FIND_IN_SET(..) function call like
SELECT tb1.*,
(SELECT GROUP_CONCAT(name) FROM tb2
WHERE FIND_IN_SET(tb2.id, tb1.ids_tb2)>0) names
FROM tb1
You can do a simple select in the in. Just like this:
SELECT tb1.*,
(SELECT GROUP_CONCAT(name)
FROM tb2
WHERE id IN(select * from tb1.ids_tb2 )) AS names
FROM tb1
Related
I am trying to get different values on rows of one table, and I can't think of how to that.
Lets say for example:
table1
id | name | empCode | insCode
1 John | E101 | I101
table2
id | code | name | role
1 | E101 | Mich | 2
2 | I101 | Bran | 3
table1 has reference id of table2, and empCode and insCode in table1 dependes on code and role in table2.
What I want is like this:
id | code | name | empCode | insCode | role |
1 | ? | John | E101 | I101 | ? |
I honestly don't know if its possible to retrieve multiple rows in same table in one select query.
If its not, what other possible solutions can I go with?
There is problem in your table design but you can get as your expected with below query:
select
result1.id,
result1.code,
result1.name,
result1.empCode,
result1.insCode,
result1.role as role_of_empCode,
result2.role as role_of_insCode
from (
SELECT tbl1.id,
tbl2.code,
tbl1.name,
tbl1.empCode,
tbl1.insCode,
tbl2.role
FROM `table2` tbl2
INNER JOIN `table1` tbl1
on tbl2.code = tbl1.empCode
) as result1,
`table2` result2
where result1.insCode = result2.code
ORDER BY result1.id;
You can check in this link!
Try something like:
SELECT a.*, b.role FROM
table1 a inner join table2 b on a.id=b.id
Also you may want to consider removing empcode and inscode from table 2 since its being redundant in table 1 OR vice versa depending on what your objectives are.
I'm trying to export multiple MySQL tables into a single CSV file, these tables have different number of columns and have nothing in common. An example is below:
table1:
ID| Name
1 | Ted
2 | Marry
null| null
table2:
Married| Age | Phone
Y | 35 | No
N | 25 | Yes
Y | 45 | No
The result that I want to get is:
ID| Name | Married | Age | Phone
1 | Ted | Y | 35 | No
2 | Marry | N | 25 | Yes
null| null | Y | 45 | No
Is it possible to do using MySQL commands? I have tried all types of join but it doesn't give me the result that I need.
Try this query:
SELECT * FROM
(SELECT #n1:=#n1+1 as 'index', Table1.* FROM Table1, (select #n1:=0)t)t1
natural join (SELECT #n2:=#n2+1 as 'index', Table2.* FROM Table2, (select #n2:=0)t1)t2 ;
You can see in this fiddle a working example.
In the example we generate an index column for Table1 and Table2 and natural join the 2 tables.
This way the join is done using the row position as returned by the SELECT of tables without any ORDER operator and usually this is not a good idea!
I'm not quite sure you understand what you are asking but sure you can join two tables without really caring on which rows will be matched:
SET #i1=0;
SET #i2=0;
SELECT * INTO OUTFILE 'xyz' FIELDS TERMINATED BY ','
FROM (SELECT #i1:=#i1+1 as i1, table1.* FROM table1) AS a
JOIN (SELECT #i2:=#i2+1 as i2, table2.* FROM table2) b ON (a.i1=b.i2);
I have a table
--------------------
ID | Name | RollNO
--------------------
1 | A | 18
--------------------
2 | B | 19RMK2
--------------------
3 | C | 20
--------------------
My second table is
-----------------------
OldRollNo | NewRollNo
-----------------------
18 | 18RMK1
-----------------------
19 | 19RMK2
-----------------------
20 | 20RMK3
-----------------------
21 | 21RMK4
-----------------------
22 | 22RMK5
-----------------------
I want the resulting table like
----------------------------------
ID | Name | RollNo | LatestRollNo
----------------------------------
1 | A | 18 | 18RMK1
----------------------------------
2 | B | 19RMK2 | 19RMK2
----------------------------------
3 | C | 20 | 20RMK3
----------------------------------
What would be the select query like? This is just the replica of my problem. I have used CASE Statement with the select query but as the records in my table is large, it's taking too much time. In my second table the OldRollNo Column is unique.One more thing is that in the resultant RollNo column if the newly assigned RollNo is already present then it should be copied exactly to the next column i.e LatestRollNo. I have to check only those RollNo which are old.
Thanks.
Try something like this:
select t1.ID
, t1.Name
, t1.RollNO
, LatestRollNO = coalesce(n.NewRollNo, o.NewRollNo)
from t1
left join t2 o on t1.RollNO = o.OldRollNo
left join t2 n on t1.RollNO = n.NewRollNo
SQL Fiddle with demo.
It sounds like your issue is performance not logic; something like this should hopefully allow approriate index usage assuming you have the appropriate indexes on t2.OldRollNo and t2.NewRollNo.
The problem with OR or CASE in a WHERE clause is that these don't always lend themselves to efficient queries; hopefully this will be a bit more useful in your case.
select f.ID, f.name, f.RollNo, s.NewRollNo as "Latest RollNo"
from FirstTable f
inner join
SecondTable s on f.RollNo = s.OldRollNo or f.RollNo = s.NewRollNo
select t.id,t.name,t.rollno,tt.newrollno as latestrollno from
talble1 t
left join
table2 tt on t.rollno = tt.oldrollno
You need to use inner join.
SELECT t1.ID,t1.Name,t2.RollNo,t2.NewRollNo AS LatestRollNo
FROM Table1 t1
INNER JOIN Table2 t2
ON t1.RollNo=t2.OldRollNo OR t1.RollNo=t2.NewRollNo
Somewhat confusing so its easier if I put down example and expected output to begin.
I have a table that could look like this: (Unit1 - Unit2 columns could span up to 30 columns in the same general format)
| ID | Name | Unit1_left | Unit2_left |
| 1 | Tom | 50 | NULL |
| 2 | Tom | NULL | 1 |
| 3 | Tom | 45 | NULL |
| 4 | Dan | NULL | NULL |
What I am trying to select is a table like this:
| Name | Unit1_left | Unit2_left |
| Tom | 45 | 1 |
| Dan | NULL | NULL |
What that is doing is grouping by name and attempting to find the last values in the 2 other columns if they exist (if not then it returns NULL).
I have looked at various other questions and they all say to use Max() however this will not work since it selects the highest value (incorrect). I have seen that in MsSQL there is a Last() function which looks vaguely like what I want it to do but its not implemented in MySQL and isn't exactly what I need anyway.
What I am trying to ask is, does anyone know of a possible method of selecting the data like this or if I will have to use a separate programming language to do this?
This will produce the result set you've described
SELECT dname.name,
l1value.unit1_left,
l2value.unit2_left
FROM (SELECT DISTINCT `name`
FROM table1) `DName`
LEFT JOIN (SELECT `name`,
Max(id) id
FROM table1
WHERE unit1_left IS NOT NULL
GROUP BY `name`) l1
ON dname.`name` = l1.`name`
LEFT JOIN table1 l1value
ON l1.id = l1value.id
LEFT JOIN (SELECT `name`,
Max(id) id
FROM table1
WHERE unit2_left IS NOT NULL
GROUP BY `name`) l2
ON dname.`name` = l2.`name`
LEFT JOIN table1 l2value
ON l2.id = l2value.id ;
DEMO
I did it by creating 2 inline views to the highest id for non-null values for both unit1_left and unit2_left (l1 and l2). Then joined it back to original table to get the values (l1value and l2value). We then join that back to a third inline view (dname) that creates the distinct names.
It's quite messy and it might make more sense just to keep your data in a more sensible manner.
You can use subqueries in you select statement. Using SqlFidlle I came up with this.
select o.name,
(select o2.Unit1_left
from original as o2
where o.name = o2.name
and o2.Unit1_left is not null
order by o2.id desc
LIMIT 1) as Unit1_left,
(select o3.Unit2_left
from original as o3
where o.name = o3.name
and o3.Unit2_left is not null
order by o3.id desc
LIMIT 1) as Unit2_left
from original as o
group by o.name
order by id;
I have two table .
first table
f_id | date |
--------------
1 |2012-1-01
1 |2012-1-02
second table
s_id | f_id | name
-------------------
1 | 1 |rakib
2 | 1 | shohug
i want to view this data
like
f_id| date | s_id | name
--------------------------
1 |2012-1-01 | 1 | rakib
1 |2012-1-01 | 2 | shohug
1 |2012-1-02 | 1 | rakib
1 |2012-1-02 | 2 | shohug
plz help me how to write it in mysql query
select f.f_id, f.`date`, s.s_id, s.`name`
from firsttable f
join secondtable s on f.f_id=s.f_id
order by f.f_id, f.`date`, s.s_id
Do you just mean
SELECT first.f_id, first.date, second.s_id, second.name
FROM first JOIN second ON second.f_id = first.f_id
?
(See http://en.wikipedia.org/wiki/Join_(SQL).)
Seems you want to do FULL OUTER join it includes all rows from both tables. Mysql does't support it directly.
But you can try below:
select * from firsttable as a
left outer join secondtable as o on a.f_id = o.f_id
union all
select * from firsttable as a
right outer join secondtable as o on a.f_id = o.f_id
Also go through this article will help you http://www.xaprb.com/blog/2006/05/26/how-to-write-full-outer-join-in-mysql/