I have two table like this :
table1
| cat | id |
|----- |------|
| 110** | 12 |
| 110** | 18 |
| 110** | 13 |
----------------
table2
| cat | qty |
|----- |------|
| 11012 | 2 |
| 11017 | 8 |
| 11016 | 1 |
----------------
result
| cat |
|----- |
| 11012 |
---------
can I combine cat,id columns in table1 (like 11012) to get the same value in cat column in table2 (like 11012)?
I tried a query like this:
SELECT a.cat,a.id FROM table1 a JOIN table2 b ON a.cat LIKE CONCAT('%', b.cat, '%')
but I didn't get the results I expected.
SELECT *
FROM table1 t1
JOIN table2 t2 ON CONCAT(TRIM(TRAILING '*' FROM t1.cat), id) = t2.cat
Logic: remove trailing asterisks from cat, concatenate id, then compare.
fiddle
If I understand question correctly, you should do
SELECT a.cat,a.id FROM table1 a JOIN table2 b ON LEFT(a.cat,3) = LEFT(b.cat, 3)
Related
if I have following table :
+------+--------+---------+-------+-------+
| id | name | Surnmae | email |address|
+------+--------+---------+-------+-------+
| 1 | | Lee | aaa |23 a st|
| 2 | a | | aaa | |
| 3 | c | | ccc | |
+------+--------+---------+-------+-------+
How can I delete duplicate record base on email but keep the record which contains most value and give the following result?
+------+--------+---------+-------+-------+
| id | name | Surnmae | email |address|
+------+--------+---------+-------+-------+
| 1 | | Lee | aaa |23 a st|
| 3 | c | | ccc | |
+------+--------+---------+-------+-------+
I got a way, but it is kind ok stupid, it works for small table just like the one I provided above:
DELETE FROM table1
WHERE firstname NOT IN (SELECT *
FROM (SELECT MAX(n.firstname)
FROM table1 n
GROUP BY n.email) x)
OR lastname NOT IN (SELECT *
FROM (SELECT MAX(n.lastname)
FROM table1 n
GROUP BY n.email) x)
OR address NOT IN (SELECT *
FROM (SELECT MAX(n.address)
FROM table1 n
GROUP BY n.email) x)
but does anyone has an idea which can simplif my query?? like one line query?
Please try this i assume ur table name is members
delete t1 from members LEFT JOIN members t1 ON members.email = t1.email AND members.id != t1.id AND IFNULL(CHAR_LENGTH(members.Surnmae ),0) > IFNULL(CHAR_LENGTH(t1.Surnmae ),0) WHERE t1.id > 0;
EDIT - I apologize but I didn't include the correct information the first time!
I have the following two tables:
table 1
+----+-------+-------+
| id | model | color |
+----+-------+-------+
| 1 | 111AA | red |
| 2 | 222BB | blue |
| 3 | 333CC | |
| 4 | 444DD | green |
+----+-------+-------+
table 2
+----+-------+-------+
| id | model | quant |
+----+-------+-------+
| 6 | 111AA | 2 |
| 7 | 222BB | 5 |
| 8 | 222BB | 3 |
+----+-------+-------+
I need a query that will take all the rows from table 1 where the color column is not empty along with the sum of the column quantity in table two that match a certain model (in the example given, model = '222BB') to produce the following table:
+----+-------+-------+------+
| id | model | color | quant|
+----+-------+-------+------+
| 1 | 111AA | red | |
| 2 | 222BB | blue | 8 |
| 4 | 444DD | green | |
+----+-------+-------+------+
This is what I tried so far:
SELECT t1.id, t1.model, t1.color, SUM(t2.quant)
FROM table1 t1 LEFT OUTER JOIN table2 t2
ON t1.id = t2.id
WHERE t1.color != '' AND t2.model = '222BB'
However, this didn't work.
Any help is greatly appreciated.
Thanks!
To receive the expected table, run the following SQL query:
SELECT t1.id, t1.model, t1.color, IF(t2.model = '222BB', SUM(t2.quant), NULL)
FROM table1 t1
LEFT JOIN table2 t2 ON t1.model = t2.model
WHERE t1.color != ''
GROUP BY t1.model
The result will be the same as in your table. But I think it would be better to update the design to make join on ID column but not model-name.
Try this,
select t1.id, t1.model,t1.color,sum(t2.quant)
from table1 t1
left outer join table2 t2 on (t1.model = t2.model and t1.color <> ‘’)
group by t1.model
In Sql, you should not write != or == with null. It is highly suggested that you use IS NULL and IS NOT NULL clause.
http://www.tutorialspoint.com/sql/sql-null-values.htm
** select a.model,b.total from (select model from table1 where color is not null) a, (select model,sum(quant) total from table2 group by model) b where a.model=b.model;
**
I am trying to concatenate 2 tables into 1 table with 1 mysql command. I also want to concatenate a file path into one path in the new table. Any information would be helpful. Thank you.
Something like this :
INSERT INTO table3 VALUES (Location) SELECT "A.Loc_Path B._FilePath" FROM Table2 A INNER JOIN table1 B ON A._Loc_ID = B._Loc_ID
I would like to take Table 1 & 2 and create Table 3
Table 1
| ID | _FilePath | _Loc_ID |
| 1 | 001\yay\txt.html | 1 |
| 1 | 002\yay\txt.php | 2 |
Table 2
| _Loc_ID | Loc_Path |
| 1 | D:\documents\test\ |
| 2 | C:\Temp\test\ |
Table 3
| Id | Location |
| 1 | D:\documents\test\001\yay\txt.html |
| 2 | C:\Temp\test\002\yay\txt.php |
Use concat
INSERT INTO table3 (Location) SELECT concat (A.Loc_Path , B._FilePath)
FROM Table2 A INNER JOIN table1 B ON A._Loc_ID = B._Loc_ID
what you need is to use the concat function to concatenate the two columns:
INSERT INTO table3 VALUES (Location) SELECT CONCAT(A.Loc_Path, B._FilePath) FROM
Table2 A INNER JOIN table1 B ON A._Loc_ID = B._Loc_ID
I'd like to query the table which stores the entries somehow mixed in rows and columns.
Here is the table:
| id | class | field | value |
|-----|-------|-------|-------|
| 1 | 1 | a | AA |
| 2 | 1 | b | BB |
| 3 | 1 | c | CC |
| 4 | 2 | a | DD |
| 5 | 2 | b | EE |
| 6 | 2 | c | FF |
What should be the query to get a result like:
a)
| class | new_a | new_c |
|-------|-------|-------|
| 1 | AA | CC |
| 2 | DD | FF |
My pseudo query I imagine it would be something like:
select class, value(where field=a) as new_a, value(where field=c) as new_c, from table;
b)
| class | new_a | new_c |
|-------|-------|-------|
| 2 | DD | FF |
For this one I guess it should be like:
select class, value(where field=a) as new_a, value(where field=c) as new_c, from table where class = '2';
Unfortunatelly I'm rarely using the mysql and I'm not sure how to build this query. All constructive suggestions are appreciated.
Try this query
For a) The query is
SELECT t1.class, t1.value as new_a, t2.value as new_b
FROM table t1
JOIN table t2 ON(t2.class=t1.class )
WHERE t1.field='a' AND t2.field='c'
For b) The query is
SELECT t1.class, t1.value as new_a, t2.value as new_b
FROM table t1
JOIN table t2 ON(t2.class=t1.class )
WHERE t1.field='a' AND t2.field='c' AND t1.class='2'
1) you are trying to convert the rows into columns so I joined the same table twice with condition as 2 tables should have same 'class' value
2) then added condition as what to fetch from table t1 and table t2 as t1.field='a' and t2.field='c'
3) In second query you need only the class value '2', so i added the condtion as t1.class=2
I couldn't quite find an answer for this so I apologize if it has been answered before.
Say I have two tables:
Table 1
+--------+----+
| Number | ID |
+--------+----+
| 1 | AA |
| 5 | BB |
| 10 | CC |
| 2 | DD |
| 20 | EE |
+--------+----+
Table 2:
+----+--------+
| ID | Name |
+----+--------+
| AA | Cat |
| BB | Dog |
| CC | Bird |
| DD | Fish |
| EE | Monkey |
+----+--------+
I want to multiple the Number column by a value (for example increase it by 50%) but I want to modify only the ones whose Name = 'Dog' only.
I tried something like this for my code:
UPDATE Table1
SET number = number*1.50
FROM Table1 JOIN Table2 USING (ID)
WHERE name = 'Dog';
But it's not working.
Any kind of help would be greatly appreciated.
EDIT:
It seems that I have found my answer:
UPDATE Table1
SET number = number*1.50
WHERE id IN (SELECT id FROM Table2 WHERE name = 'Dog');
Thank you :)
maybe something like this?
UPDATE Table1 t1 JOIN Table2 t2 ON t1.ID=t2.ID
SET t1.number = t1.number*1.50
WHERE t2.name = 'Dog';