lets take an example - i have 2 data tables, table "books" with columns "shelfId" and "text", and table "shelves" with column "Id". I want to join these two tables on books.shelfId == shelves.Id, and as a result, i want to see a new table with 2 columns - column 1 has unique values of Ids, and column 2 has merged values of books.text with same books.shelfId values and separated by comma or something else, i.e. :
Is it possible to write such sql select to get what i need ?
Here is fiddle http://sqlfiddle.com/#!2/c96dfa/1
SELECT shelfid as id, GROUP_CONCAT(text) AS text
FROM books
GROUP BY shelfid
Related
Let's assume I have those datasets:
Table A:
id (int)
value (varchar)
b_ids(varchar)
1
a value
1
2
another value
2,3
Table B:
id (int)
value (varchar)
1
a value
2
another value
3
another another value
The reason I have to use b_ids here is because the B rows have to be inserted before the A rows
I am trying to SELECT rows from Table A and the corresponding values from Table B in one single query, and make that query a view for filtering purposes
My attemps so far only gave me back the A rows + the first value from the related B rows:
SELECT * FROM A
LEFT JOIN B ON B.id IN (A.b_ids);
And I obtained something like this:
id
value
b_ids
id
value
1
a value
1
1
a value
2
another value
2,3
2
another value
I have tried other joins (INNER JOIN, RIGHT JOIN, CROSS JOIN, LEFT OUTER JOIN, RIGHT OUTER JOIN), with the same result
I obviously am still missing something in the joins department if my endeavor is even possible to do in one single SELECT
Is there a way to return the values of Table B as an array of rows in this query?
Even if the result below is the output, I can work with it:
id
value
b_ids
id
value
1
a value
1
1
a value
2
another value
2,3
2
another value
2
another value
2,3
3
another another value
Note: I have chosen Table A as the first table here because the real case involves joins with other tables
you should change tables schema
if there is one to many relationship between A(1)---(n)B
tables should be like this:
Table A:
id (int)
value (varchar)
1
a value
2
another value
Table B:
id (int)
value (varchar)
a_ids(varchar)
1
a value
1
2
another value
2
3
another another value
2
so now you can define tables relationship or fetch data as you need in single query easily
** if tables has many to many relation you need a pivot table.
I am trying to write a SQL Query. I have 2 tables. Table 1(left table) and Table 2(right table). I want to do a left join. So If a Group in table 1 is found in table 2, we replace it with New Group.
Table 2 has all PRIME Group. There are 2 conditions:
If a PRIME (or) SEMIPRIME is there in table 1, we lookup in table 2 and replace group with new group if found.
If a PRIME is there in table 1,and does not exist in NewGroup(Table2) we omit that group itself.(highlighted in yellow).
I tried using coalesce(y.Newgroup,x.Group), but how do I include 2 conditions?
Please refer input tables and expected output here
I created table here: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=f019fb942f3aae3d62427a0ac142d639
I need some assistance with deleting data within an SQL Table if it matches data from another table.
There are two Tables
Table 1: DNC
Table 2: Call_Logs
Table 1 has only one column called phone_number.
Table 2 has multiple columns, but the main one that is important is also named phone_number.
Basically, I want to remove any numbers that are in Table 2 from Table 1, if they exist. Now, I don't want to delete every number from Table 1 if they exist in Table 2. What numbers I collect from Table 2 are based on some criteria.
To pull the data from Table 2 that I need to delete from Table 1, I use the following:
select phone_number from call_logs where call_date < 'DATE' and Status = 'DNC'
This query will give me a list of all phone numbers that I would want to remove from Table 1 if it exists.
EXAMPLE:
https://drive.google.com/file/d/0B4NE4ZDXd6steW5odWhBMDJSY1U/view
I am not sure how I would go about running the query in SQL. Any types would be appreciated it.
Looking to your sample in img
You could use a left join on table 2 (where table2.phone_number is null alias don't match)
delete from table1
left join table2 on table1.phone_number = table2.phone_number
where table2.phone_number is null
correlated subquery w/ an exists so it can early exit
The select 1 in the subquery is because we have to select a value but it doesn't matter what value that is. since the coloration (DNC.Phone_Number = CL.Phone_Number) is all we are after; along with your limits on call_log.
DELETE
FROM DNC
WHERE exists (SELECT 1
FROM Call_logs CL
WHERE CL.call_date < 'DATE'
and CL.Status = 'DNC'
and DNC.Phone_Number = CL.Phone_Number)
I have a database table that contains person's fingerprints (template column here), each person can introduce 2 fingers, so the same person must have 2 records.
Here is the table :
Person with ID '275' have 2 records, each for a single finger.
Now I am using Talend to create a table so I can merge every two fingerprints in a single one, I mean row n°37 and 38 will be in single row and template column will be concatenated to have only one person_id
You can try something like this:
INSERT INTO newtable (ids, person_id_integer)
SELECT CONCAT(finger1.id, "|", finger2.id), finger1.person_id_integer
FROM oldtable finger1, oldtable finger2
where finger1.person_id_integer = finger2.person_id_integer
and finger1.id <> finger2.id
Of course, depnds on how you want to store the new data
:)
I found a solution :
I added the same table twice as an input and output like shown in this photo and first fetched it with this query :
select FP_ID, FP_DEVICE, group_concat(FP_FINGER_PRINT SEPARATOR ''),FP_EMPLOYEE_ID
from t_finger_print GROUP BY FP_EMPLOYEE_ID;
is there any way, i can get a field values (comma seperate) into an array or temporary table.
ex: i have field following with values 3,7,23,45
i want get them into an array without using PHP, or into a temporary table.
as i need to do some joint queries based on those values.
any help is appreciated
thanks
my table name is: shoes
field name is following
sample table values are like these
+----------+-----------+
userId following
+----------+-----------+
1 5,7,8,12
2 5,2,1,67
now, when i search for userId 1, i want to get values 5,7,8,12 into an array or temp table.
It is possible although not particularly quick. The best solution is to redesign the database to move the comma separated list into a different table, with one row for each comma separated element.
However if you want an SQL way to do it then something like the following will do it (this relies on having a table called integers with a single column called i, with 10 rows with the values 0 to 9).
SELECT DISTINCT shoes.userId , substring_index(substring_index(shoes.FollowingIdString, ',', anInt), ',', -1) AS SplitField
FROM shoes,
(SELECT a.i+b.i*10+c.i*100 AS anInt
FROM integers a, integers b, integers c) Sub1
HAVING SplitField IS NOT NULL
Assuming you have a table PersonList which has multiple columns and this is the table where you want to join to get the name of the followers list. eg
PersonTable
ID Name
1 AAA
2 BBB
there is a mysql function called FIND_IN_SET, eg
SELECT a.userID, GROUP_CONCAT(b.Name)
FROM Follower a
INNER JOIN Person b
ON FIND_IN_SET(b.id, a.following) > 0
GROUP BY a.userID
SQLFiddle Demo