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
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 have a table (simplified) that looks like this:
id | name | selfreference | selfreference-name
------ | -------| --------------| ------------------
1 | Vienna | |
2 | Wien | | Vienna
3 | Виена | | Vienna
The selfreference column refers to the id numbers of the same table. In the above example, both Wien and Виена refer to the same city, so the value of their selfreference column should be equal to 1.
In other words, I need to do something like
update `places`
set `places`.`selfreference` =
(select `places`.`id` from `places`where `places`.`name` = `places`.`selfreference-name`)
but the SELECT statement above is obviously wrong. I am at a loss how to proceed.
Any tips would be greatly appreciated.
All best,
Tench
Edit: the desired output would look like this:
id | name | selfreference | selfreference-name
------ | -------| --------------| ------------------
1 | Vienna | |
2 | Wien | 1 | Vienna
3 | Виена | 1 | Vienna
Could be you need a self join
chekc with select
select a.*, b.*
from `places` as a
inner join `places` as b
where b.`name` = a.`selfreference-name`;
and then if the query above give you the right result
update `places` as a
inner join `places` as b
set b.`selfreference` = ab.`id`
where b.`name` = a.`selfreference-name`;
The following query does the job:
UPDATE places p1
INNER JOIN places p2 ON p1.`name` = p2.`selfreference-name`
SET p2.selfreference = p1.id;
p2 -> instance of table places which will be updated.
p1 -> instance of table places from where the id of the matching selfreference-name is taken.
WORKING DEMO BEFORE UPDATING
WORKING DEMO AFTER UPDATING
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);
can i use GROUP_CONCAT to update table? I have 2 tables
i
d | label
------------------------------
1 | ravi,rames,raja
------------------------------
2 | ravi
------------------------------
3 | ravi,raja
------------------------------
4 | null
------------------------------
5 | null
------------------------------
6 | rames
------------------------------
and
id | values
------------------------------
12 | raja
------------------------------
13 | rames
------------------------------
14 | ravi
------------------------------
And i want the result like following table--
id | label
------------------------------
1 | 12,13,14
------------------------------
2 | 14
------------------------------
3 | 14,12
------------------------------
4 | null
------------------------------
5 | null
------------------------------
6 | 13
------------------------------
but by using the following query -
SELECT `table1`.`id`, GROUP_CONCAT(`table2`.`id` ORDER BY `table2`.`id`) AS label
FROM `table1`
JOIN `table2` ON FIND_IN_SET(`table2`.`values`, `table1`.`nos`)
GROUP BY `table1`.`id`;
Im getting-
id | label
------------------------------
1 | 12,13,14
------------------------------
2 | 14
------------------------------
3 | 12,14
------------------------------
6 | 13
------------------------------
I want to keep the null value. otherwise the order of rows will be broken. please help.
sorry for the large font :(
You just need a LEFT JOIN to preserve the nulls:
SELECT `table1`.`id`, GROUP_CONCAT(`table2`.`id` ORDER BY `table2`.`id`) AS label
FROM `table1`
LEFT JOIN `table2` ON FIND_IN_SET(`table2`.`values`, `table1`.`nos`)
GROUP BY `table1`.`id`;
However, I recommend against updating a table to include comma-separated values in a column. It forces you to use FIND_IN_SET() when querying it, and breaks the ability to index the column, affecting the performance of your queries. The more sustainable action would be to normalize table1 so that it doesn't include a comma-separated column.
Update:
To use GROUP_CONCAT() in an UPDATE statement, you would use a syntax like the following. Substitute your correct table and column names, and in your case, you probably want to replace the entire JOIN subquery with your SELECT statement.
UPDATE
tbl_to_update
JOIN (SELECT id, GROUP_CONCAT(concatcolumn) AS label FROM tbl GROUP BY id) tbl_concat
ON tbl_to_update.id = tbl_concat.id
SET tbl_to_update.column_to_update = tbl_concat.label
WHERE <where condition>
So in your case:
UPDATE
table1
INNER JOIN (SELECT id, GROUP_CONCAT(id) AS label FROM table1 GROUP BY id) table2
ON FIND_IN_SET(`table2`.`label`, `table1`.`nos`)
SET table1.nos = table2.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/