I have a table which uses external data that arrives cut off. Can I complete it from tables that aren't cut off (as they use alternative external data)?
In this example, the 2nd and 3rd rows can be completed using the matching data from their respective tables.
Table1:
Table | Type | Description
X | A | A long line th
X | B | A long line th
Y | B | This line d
Y | A | This line d
TableX:
Type | Description
A | A very long line that doesn't get cut off
B | A long line that doesn't get cut off
TableY:
Type | Description
A | just a long line that doesn't get cut off
B | This line doesn't get cut off
Needed result
===============
Table1:
Project | Type | Description
X | A | A long line th
X | B | A long line that doesn't get cut off
Y | B | This line doesn't get cut off
Y | A | This line d
try something like this...
SELECT `Table` as Project,Type,Description
FROM Table1 T1 WHERE Type = 'A'
UNION ALL
SELECT `Table`,T1.Type,T2.Description
FROM Table1 T1 INNER JOIN TableX T2
ON T1.`Table` = 'X' AND T1.Type = 'B' AND T1.Type = T2.Type
UNION ALL
SELECT `Table`,T1.Type,T2.Description
FROM Table1 T1 INNER JOIN TableY T2
ON T1.`Table` = 'Y' AND T1.Type = 'B' AND T1.Type = T2.Type
http://www.sqlfiddle.com/#!9/b40a3/6
SELECT `Table` as Project,Type,Description
FROM Table1 T1 WHERE Type = 'A'
UNION ALL
SELECT `Table`,T1.Type,T2.Description
FROM Table1 T1 INNER JOIN TableX T2
ON T1.`Table` = 'X' AND T1.Type = 'B' AND T1.Type = T2.Type
AND T2.Description like CONCAT(T1.Description,"%")
UNION ALL
SELECT `Table`,T1.Type,T2.Description
FROM Table1 T1 INNER JOIN TableY T2
ON T1.`Table` = 'Y' AND T1.Type = 'B' AND T1.Type = T2.Type
AND T2.Description like CONCAT(T1.Description,"%")
If you're trying to match description too you can use something like
http://www.sqlfiddle.com/#!9/b40a3/9
To complete rows you can select and update Description.
update Table1 set Description = (select Description from TableX where Description LIKE 'A long line th%' limit 1);
update Table1 set Description = (select Description from TableY where Description LIKE 'This line d%' limit 1);
To select without update table you can
select tb1.Table as Proyect, tb1.Type, (select Description from TableX where Description like CONCAT(tb1.Description,'%')) from Table1 tb1 where tb1.Table = 'X'
UNION
select tb1.Table as Proyect, tb1.Type, (select Description from TableY where Description like CONCAT(tb1.Description,'%')) from Table1 tb1 where tb1.Table = 'Y';
And this maybe can help someone,
INSERT INTO Table1 (
select 'X' as Project, Type, Description from Table1
UNION
select 'Y' as Project, Type, Description from Table2);
Maybe your Table1.Description is wrong.
ALTER TABLE Table1 MODIFY COLUMN Description varchar(200);
Related
i have tables like below,
Table 1 :
ID
FIELD_NAME
1
field_1
2
field_2
3
field_3
Table 2 :
ID
TAG_NAME
1
tag_1
2
tag_2
3
tag_3
4
tag_4
Table 3 :
FIELD_ID
TAG_ID
1
1
1
2
1
3
2
1
3
2
3
3
so table-3 shows :
field1(from table1) connected with all 3 tag id's of table-2
field2(from table1) connected with all tag id=1 of table-2
and
field3(from table1) connected with all tag id=2,3 of table-2
I like to write a MYSQL Query to fetch
for a given tag_name, find all field name which are NOT associated with this
Example :
input = tag_1
output = field_3
Explanation : as field_3 not have any relation with tag_1 id
input = tag_4
output = field_1, field_2, field_3
Explanation : as no field have any relation with tag_4 id
You could check for fields where certain link table entries do not exist...
SELECT
*
FROM
table1
WHERE
NOT EXISTS (
SELECT *
FROM table3
WHERE field_id = table1.id
AND tag_id = (SELECT id FROM table2 WHERE tag_name = 'tag_4')
)
(Please excuse typos, I'm on my phone.)
dbfiddle as borrowed from #nbk : here
You can do it with a CROSS join of table1 to table2 and a LEFT join yo table3 where you return the unmatched rows:
SELECT t2.TAG_NAME, t1.FIELD_NAME
FROM table1 t1 CROSS JOIN table2 t2
LEFT JOIN table3 t3 ON t3.FIELD_ID = t1.ID AND t3.TAG_ID = t2.ID
WHERE t2.TAG_NAME = ? AND t3.FIELD_ID IS NULL
If you want the results as comma separated list you can also group by TAG_NAME and use GROUP_CONCAT():
SELECT t2.TAG_NAME, GROUP_CONCAT(t1.FIELD_NAME) fields
FROM table1 t1 CROSS JOIN table2 t2
LEFT JOIN table3 t3 ON t3.FIELD_ID = t1.ID AND t3.TAG_ID = t2.ID
WHERE t2.TAG_NAME = ? AND t3.FIELD_ID IS NULL
GROUP BY t2.TAG_NAME
Replace ? with the tag that you search for.
See the demo.
I'm writing a php code to calculate a value. I have 4 tables with 2 column each. their headers are same. I want to fetch second column of tables that have, say 'MAN', value on their first column:
here are my tables and their dummy values:
I joined tables on 'dst' with a where clause but the problem arises when a table has does not have a common value and we have not full outer join in mysql. I know we can simulate this with union all operation but I want a efficient way to do this.
here is my try:
select t1.dst, t1.pay as pay, t2.pay as pay2, t3.pay as pay3, t4.pay as pay4 from t1 left outer join t2 on t1.dst = t2.dst left outer join t3 on t3.dst=t2.dst left outer join t4 on t1.dst=t4.dst where t1.dst='man';
it's dummy because left outer join is not true for this purpose except special cases.
actually I want this:
First union then pivot
drop table if exists t,t1,t2,t3;
create table t (dst varchar(3),value int);
create table t1 (dst varchar(3),value int);
create table t2 (dst varchar(3),value int);
create table t3 (dst varchar(3),value int);
insert into t values ('abc',10),('man',10);
insert into t1 values ('abc',10),('man',5);
insert into t2 values ('abc',10),('man',10);
insert into t3 values ('abc',10);
select dst,
MAX(CASE WHEN tbl = 't' then value end) as t1,
MAX(CASE WHEN tbl = 't1' then value end) as t1,
MAX(CASE WHEN tbl = 't2' then value end) as t2,
MAX(CASE WHEN tbl = 't3' then value end) as t3
from
(
select 't' as tbl,dst,value from t where dst = 'man'
union
select 't1' as tbl,dst,value from t1 where dst = 'man'
union
select 't2' as tbl,dst,value from t2 where dst = 'man'
union
select 't3' as tbl,dst,value from t3 where dst = 'man'
) s
group by s.dst;
+------+------+------+------+------+
| dst | t1 | t1 | t2 | t3 |
+------+------+------+------+------+
| man | 10 | 5 | 10 | NULL |
+------+------+------+------+------+
1 row in set (0.00 sec)
I have multiple table like this :
I want the result like in the picture if I use 'where table1.id = 1' :
table2 is store value and key of columns name
table3 is store columns name that I need to show
The result should be
ID | Name | A | B | C
1 | a | 10 | 20 | 30
You need little JOINs with conditional aggrgation :
select t1.id, t1.name,
sum(case when t3.name = 'a' then t2.value else 0 end) as A,
sum(case when t3.name = 'b' then t2.value else 0 end) as B,
sum(case when t3.name = 'c' then t2.value else 0 end) as C
from table1 t1 inner join
table2 t2
on t2.table1_id = t1.id inner join
table3 t3
on t3.key_id = t2.key
where t1.id = 1
group by t1.id, t1.name;
However, this would do aggregation with only known values prior, if you want go with dynamic way, then you might need programming approach in MySQL.
I have this table
Table1
id | name |
1 | a |
2 | c |
and other table like this
Table2
id | name |
1 | b |
2 | d |
How can I sort this two table to result
a b c d
DONE READING THIS but it will only gives me this result
a c b d
when I try this
SELECT * FROM Table1 AS t1 JOIN Table2 AS t2 ON t2.id = t1.id
ORDER BY t1.name ASC, t2.name ASC;
You can try taking a UNION of the two tables instead:
SELECT id, name
FROM Table1
UNION ALL
SELECT id, name
FROM Table2
ORDER BY name
If you want to retain information about the original source of each record, you can add a column for that:
SELECT id, name, 't1' AS source
FROM Table1
UNION ALL
SELECT id, name, 't2'
FROM Table2
ORDER BY name
Update:
If Codeigniter does not support UNION out of the box, you can always put the above query into a string and execute it natively:
$sql = "SELECT id, name FROM Table1 UNION ALL SELECT id, name FROM Table2 ORDER BY name";
$this->db->query($sql);
SELECT *
INTO #TEMP_TBL
FROM Table1 AS t1
JOIN Table2 AS t2
ON t2.id = t1.id
SELECT *
FROM #TEMP_TBL
ORDER BY ID, NAME
DROP TABLE #TEMP_TBL
If you want to use join then try below query
select * from (select x.* from table1 as x left join table2 as y on x.id = y.id) as z order by z.name;
We can use this also:
SELECT * FROM (
SELECT id, NAME FROM Table1
UNION ALL
SELECT id, NAME FROM Table2) t ORDER BY name
Question:
Table 1: id.1 | name.Joe | etc.Some | ...Other | ...Data
Table 2: id.X | number.+1 123 555 9999 | useridfromtable1.1 -> Linking telefone with Joe
Table 3: id. X | number.+1 123 555 9999 | calls.55
I need a query that join the 3 tables and I only have the id (userid) from the table 1.
So, I need from userid -> grab the telephone -> from the telefone grab calls on table3.
Answer:
SELECT t1.name,t1.id,t2.number,t3.calls
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
New Question?
Any chance you know how can I print the sum of calls on the result? after all these join I get about 10 lines of the same user and their respective calls based on the phone, what is correct for what I asked, now I need return all the calls in 1 line with the values:
sum | user ID | user Name
Maybe this will be useful:
SELECT SUM(t3.calls) FROM table1 t1 INNER JOIN table2 t2 ON t2.useridfromtable=t1.id INNER JOIN table3 t3 ON t3.number = t2.number
Try this query:
SELECT sum(t3.calls), t1.id, t1.name
FROM table1 t1
INNER JOIN table2 t2 ON t2.useridfromtable=t1.id
INNER JOIN table3 t3 ON t3.number = t2.number
GROUP BY t1.id