Retrieve all rows in table B related to row in table A - mysql

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.

Related

SQL Query on replacing element

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

MYSQL - Removing number from One Table in SQL if it exists in another Table

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)

sql join 2 tables on column x and merge field z

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

MySQL combine result only if result exist in second table

I have one table with unique IDs and second table where these IDs have stored different values. Second table might have several rows with the ID from first table. I want to get print out complete content of table #one and have one more additional column containing only if one of ID matching ID from table one has * (star) character stored.
First Table
|id1|value1|value2|value3|value4|
Second table
|id2|value1|value2|id1|value4|
| | | | | *|
Desired output
|id1|value1|value2|value3|value4|value5 with * or empty
What would be the mysql syntax?
select *, s.value4
from first_table f
left outer join second_table s
on f.id1 = s.id1
and s.value4 = "*"

How to store category specific fields in MySQL?

Let's say I have a categories table that stores categories. It is implemented in a nested set style(with left and right values).
category_id lft rgt
1 1 6
2 2 5
3 3 4
So category 1 is a parent of category 2. category 2 is a parent of category 3. So its essentially one path from root to leaf.
The category fields of category 1 should be inherited by category 2 which in turn would be inherited by category 3
Now what is the best way to store the fields for a specific category? My solution was to make another table which has the category id foreign key and the fieldname.
category_id fieldname
1 field1
1 field2
2 field3
3 field4
My problem with this approach is that when getting the fields of category 3, I need to get its parent, its parent's parent and so on until I get to the root node so that I can inherit their fields. It's not really a bad solution but I wonder if this would work when the category table is very large.
So the problem is basically an optimization problem. Is this an optimal solution?
You can do this using the schema that you have, but joining the two tables together. The beauty of the left/right nest structure is that in one query you can pull out lots of information about the whole hierarchy.
In your instance, you want to pull out all the category IDs with a 'lft' equal to or less than the 'lft' value for your given level of hierarchy, and join the results against the category ID fields in your fields table.
The query is something like:-
select table2.fieldname from table2 left join table1 on table1.category_id = table2.category_id where table1.lft <= [lft value for given level of hierarchy]
If you only have the category ID to go on then you can also extract the lft value using a subselect or joining the table back on itself.
i simply save such data in a table which has following schema:
CategoryID
ParentCategoryID
Path
so you could have
1
0
1\
then
2
1
1\2
then
3
2
1\2\3
you can then fire a simple query to either get the immediate parentid or get all the hierarchy right from the root to leaf using the path field
different but simple approach which has been working for me since last 4+ years without any issues :-)