I am trying to work out how to perform an SQL UPDATE table column from Table A into Table B. The problem I have is trying to concat multiple values from Table A Column X into Table B Column Y
TableA Structure
id | Interest
1 | Bowling
2 | Swimming
1 | Basketball
TableB Structure
id | Interest_new
1 | null
2 | null
I want Table B to have following data
TableB
id | Interest_new
1 | Bowling,Basketball
2 | Swimming
This is my attempt with SQL query but it doesn't concat , just updated table B with first match
UPDATE TableB
INNER JOIN TableA ON TableB.id= TableA.id
SET TableB.id=CONCAT(TableA.id, ',')
where TableA.id= TableB.id;
You probably intend to use GROUP_CONCAT here, as you want an aggregated CSV output:
UPDATE TableB b
INNER JOIN
(
SELECT id, GROUP_CONCAT(Interest ORDER BY id) Interests
FROM TableA
GROUP BY id
) a
ON a.id = b.id
SET
Interest_new = a.Interests;
However, I actually advocate not even doing this, as storing CSV in your SQL tables is a generally bad idea. Consider just making a view of this data instead:
CREATE VIEW newInterests AS
SELECT id, GROUP_CONCAT(Interest ORDER BY id) Interests
FROM TableA
GROUP BY id;
Related
I have a problem creating the SQL query which will select only these distinct records from table A if offer_id column in table A doesnt exists in table B column. How should I do this?
Table A construction:
--------------------------
id | offer_id | user_id
--------------------------
Table B construction
-------------------------------------
id | offer_id | user_id | date
-------------------------------------
So basically I want only select this users records from table A if they were not added to the table B which represents the prove of some action.
Right now I'm selecting distinct records from table A like below but how to make a condition if user_id exists in table B so the data would not be selected?
SELECT DISTINCT offer_id FROM aukcje_licytacja_historia WHERE user_id = :user_id ORDER BY offer_id DESC
You have pretty much described the solution:
select a.*
from a
where not exists (select 1 from b where b.offer_id = a.offer_id);
However, I suspect that you also want user_id to match:
select a.*
from a
where not exists (select 1
from b
where b.offer_id = a.offer_id and
b.user_id = a.user_id
);
Input two table, TABLE A and TABLE B
TABLE A TABLE B
A_ID | A A_ID | B
1 | a 1 | b
2 | a1 1 | b1
3 | a2 2 | b2
Expecting Output TABLE C
TABLE C
A_ID | A | C
1 | a | b,b1 <--- Concat all rows in TABLE B with ','
2 | a1 | b2
3 | a2 | NULL <--- NULL if no matched A_ID in TABLE B
Column C finds all matched A_ID and concat All rows of B in to new TEXT field.
Can this be done only with MySQL query?
insert into TABLE_C
SELECT
A1.A_ID,
A1.A,
GROUP_CONCAT(B1.B) as 'C'
FROM `TABLE_A` A1
LEFT JOIN `TABLE_B` B1
ON A1.A_ID=B1.A_ID
GROUP BY A1.A_ID;
left join on above sql helps to get the common matching (relating) rows from two tables 'TABLE_A' and 'TABLE_B' and group by id helps to get the rows which are not not matched (not relate to each other which returns the result set for 2,a1 and 3,null). group_concat bind two columns and by default it is comma separated . If we want some other separator then only we need to used group_concat with separator statement. And also insert select helps to insert a results row into a new table 'TABLE_C' which is ofcourse need to created on prior
Try this using GROUP_CONCAT() function.
SELECT
tbla.A_ID,
tbla.A,
GROUP_CONCAT(tblb.B)
FROM `TABLE A` tbla
LEFT JOIN `TABLE B` tblb
ON tbla.A_ID=tblb.A_ID
GROUP BY tbla.A_ID;
Use group_contact to aggregate values of column B in TABLE B within a group. Insertion of values to TABLE C can be done using INSERT .. SELECT
insert into tablec
select a.a_id, a.a, group_concat(b separator ',')
from tablea a
left join tableb b on (a.a_id = b.a_id)
group by a.a_id, a.a;
I have one table of like this
user|itemID
and another table like this:
itemID | itemTrait1 | itemTrait2 etc...
I am trying to link the tables in one query which should be simple. I conduct the query like so:
SELECT * FROM Table1, Table2 WHERE Table1.userID = 1 AND Table1.itemID = Table2.itemID;
The issue is that I am getting 456 results returned but if I simply run:
SELECT * FROM Table1 WHERE userID = 1;
I get 434 results. Should both these statements get the same number of results returned?
How I imagine this call working is that for every entry in Table 1 for user 1 it connects it to the item's data in Table2? I think I am missing something here.
Query to combine both tables based in itemID
select * from table1 t1
left join table t2 on t1.itemID = t2.itemID
where t1.itemID = 1;
if you are getting the more records than the records that of table1 then your second table table2 must have multiple records for itemID. for example
Table1
user | itemId
---------------
1 | 1
Table2
itemId | itemTrait1 | itemTrait2
----------------------------------------
1 | A | B
1 | C | D
now when you will apply join here it will join itemID 1 with two items of second table.
I have two tables one with ID and NAME
table 1
ID | NAME
1 | first
2 | second
3 | third
and an XREF table with ID and PARENT ID
table2
ID | PARENT ID
1 | 0
2 | 1
3 | 2
and I would like to retrieve the NAME twice like this: NAME | PARENT NAME
If it is possible to go three levels deep but with same 2-column table like this:
result table
NAME | PARENT NAME
first | NULL or EMPTY or this line the not showing at all
second | first
third | second
... then I'd like to figure that out as well.
select t1.Name, t12.Name from
table1 t1
inner join table2 t2 on t1.ID = t2.ID
inner join table1 t12 on t2.ParentID = t12.ID
This would only return 2 rows. If you want to have the first row (for ID=1) you just need to outer join instead.
Consider putting the parentid in the first table as a self-referential relationship rather than having a separate table for it.
Ex.:
table1
ID | PARENTID | NAME
---------------------------
1 NULL first
2 1 second
3 2 third
That way you would only need to join the table on itself rather than going through a 3rd table. (This is however assuming that the rows in table1 can only have a single parent, whereas your design allows one row to have multiple parents at a time)
But for your table structure, this will work:
SELECT
a.name,
c.name AS 'PARENT NAME'
FROM
table1 a
LEFT JOIN
table2 b ON a.id = b.id
LEFT JOIN
table1 c ON b.parentid = c.id
But if you made the parentid in the same table referencing id, the SQL would be reduced to this:
SELECT
a.name,
b.name AS 'PARENT NAME'
FROM
table1 a
LEFT JOIN
table2 b ON a.parentid = b.id
I have the following two tables:
TableA
article_id | attribute_id
1 | 5
2 | 6
TableB
attribute_id | attribute_name
5 | foo
6 | bar
How would I get the corresponding row if I only know the article id? So if I pass article_id 1 I get:
article_id | attribute_id | attribute_name
1 | 5 | foo
I know I could do it with two separate queries but was wondering if it could be done in one? I thought about using INNER JOIN ON but article_id isn't in both tables?
Thanks
For sure, you can (and have to) use INNER JOIN.
SELECT TableA.article_id, TableB.*
FROM TableA
INNER JOIN TableB ON TableA.attribute_id = TableB.attribute_id
WHERE TableA.article_id = 1
The SELECT part let us retrieve article_id from the first table, and all fields from the second one.
The INNSER JOIN clause joins rows that have the same attribute_id from the two tables.
The WHERE condition let us select only the row with article_id = 1 in first table.
Use NATURAL JOIN - Wikipedia Entry
SELECT *
FROM TableA NATURAL JOIN TableB
WHERE article_id = 1
select article_id, tablea.attribute_id,attribute_name
from tablea,tableb where
tablea.attribute_id=tableb.attribute_id
and article_id= :passedId
Join using attribute_id
SELECT * FROM TableA A, TableB B where
A.attribute_id = B.attribute_id