Fetching crossed records from the database - mysql

I have the following table structure:
structure_id | substructure_id | hash_id
1 1 1
1 1 2
1 2 1
2 1 3
2 1 1
2 2 2
And I want to fetch an intersection for each structure id how many hashes intersect with other structure ids, with the same hashes in different substructures being distinct. What I mean is the following output:
structure_id | structure_intersected_id | count
1 1 2
1 2 2
2 2 3
2 1 2
Is this possible to achieve with MySQL? So far I have the following query:
select t1.structure_id, t2.structure_id, count(*)
from table t1
inner join table t2 on t1.hash_id = t2.hash_id
group by t1.structure_id, t2.structure_id;
But it doesn't remove duplicated substructure hashes, and is not correct for 1-1, 2-2, 3-3, etc. structure_id pairs.
How can I solve this?

Related

MySQL - select distinct value from two column

I have a table with the following structure:
IdM|IdS
-------
1 | 2
1 | 3
1 | 4
2 | 1
2 | 3
2 | 4
3 | 1
3 | 2
3 | 3
3 | 4
How could I make a select statement on this table, which will return some rows of this table, where in each row, a specific id appears only one, indifferent on which column it is specified?
For the above result set, I would like a query that would return:
-------
1 | 2
3 | 4
-------
To give another example, if you would omit the first row in the original dataset:
IdM|IdS
-------
1 | 3
1 | 4
2 | 1
2 | 3
2 | 4
3 | 1
3 | 2
3 | 3
3 | 4
the result set should be:
-------
1 | 3
2 | 4
-------
That's actually an interesting problem. If I follow you correctly, you want to iterate through the dataset and only retain rows where both values were never seen before. You could use a recursive query:
with recursive
data as (
select idm, ids, row_number() over(order by idm, ids) rn
from mytable
where idm <> ids
),
cte as (
select idm, ids, rn, 1 as to_keep , concat(idm, ',', ids) visited from data where rn = 1
union all
select d.idm, d.ids, d.rn,
(not find_in_set(d.idm, c.visited) and not find_in_set(d.ids, c.visited)),
case when (not find_in_set(d.idm, c.visited) and not find_in_set(d.ids, c.visited))
then concat_ws(',', c.visited, d.idm, d.ids)
else c.visited
end
from cte c
inner join data d on d.rn = c.rn + 1
)
select idm, ids from cte where to_keep
The first CTE enumerates the rows ordered by both columns. Then the recursive query walks the resultset, checks if both values are new, and sets a flag accordingly of the columns. Flagged numbers are retained to be used for filtering in the following iteration.
Demo on DB Fiddle
Note that, given your requirement, not all values may appear in the resultset. Consider the following dataset:
idm ids
+-----+---
1 2
1 3
1 4
Your logic will only return the first row.

MySQL row to columns

I have searched all over the internet and I have had no luck in finding the answer to my question. I hope this has not specifically been posted yet.
I am trying to change the data from row to columns.
Here is my current query:
SELECT * FROM wp_rg_lead_detail ORDER BY id
Gives me the result below:
id lead_id form_id field_number value
1 1 1 1 A
2 1 1 2 B
3 1 1 3 C
4 2 1 1 A
5 2 1 2 B
6 2 1 3 C
The lead_id is the spesific entry and the field number is the order of the value.
The result I am looking for is:
|Column 1|Column 2|Column 3|
----------------------
| 1 | 2 | 3 |
| A | B | C |
If my tables is confusing, please see attached images here:
Current result
Wished result
Any help or pointers would be highly appricated.
u can choose what u want to show in sql command.
SELECT `value`,`field_number` FROM wp_rg_lead_detail ORDER BY id
but if u want to change schema of table it's not work.
u can retrieve data with select query thene insert what u want in new table.

Show single set of results from two tables

I am trying to show the results of items from one table where the count from another table equals a number from the first. I have been stuck on how to go about doing this for a couple weeks now so iv finally decided to ask for help. Im having a hard time explaining exactly what it is i need but i will try my best.
I am using PDO to interact with my database which is mysql.
For instance i have two tables:
table 1
-----------------
key | name | total
1 | item 1 | 3
2 | item 2 | 4
3 | item 3 | 2
table 2
-----------------
key | table1 key
1 | 1
2 | 2
3 | 3
4 | 1
5 | 1
6 | 3
7 | 2
8 | 2
So in this case there would be 3/3 items for item 1, 3/4 items for item 2, and 2/2 items for item 3. So it would show item 1 and item 3 as a result because the count for those two equal the total from table one.
I hope I explained this well enough.
If you want a sql query to do that, try this:
select t1.*
from table1 t1
inner join (
select table1_key, count(1) as cnt from table2 group by table1_key
) t2 on t1.key = t2.table1_key and t1.total = t2.cnt
SQLFiddel Demo

MySQL query to pre-populate join table with all id combinations from two other tables

I have the following query which populates a join table with all 'theme' options for a given user.
INSERT INTO join_themes_users (join_theme_id, join_user_id);
SELECT theme_id, 1
FROM themes
What I now need to do is edit this query to populate the join table with all theme options for all users
Can this be done in a single query or will I need to create a for loop in PHP?
The above query produces results looking something like...
join_id | user_id | theme_id
----------------------------------------------------
1 1 1
2 1 2
3 1 3
...
What I need is something like this...
join_id | user_id | theme_id
----------------------------------------------------
1 1 1
2 1 2
3 1 3
...
14 2 1
15 2 2
16 2 3
...
27 3 1
28 3 2
29 3 3
...
To get all combinations you cross join the tables:
INSERT INTO join_themes_users (join_theme_id, join_user_id)
SELECT themes.theme_id, users.user_id
FROM themes
CROSS JOIN users

MySQL get all unique values between two columns

I have a table with two columns that contain ID's. I want to query the table to show one list of each ID from the two columns.
TABLE
A | B
-----
1 | 2
2 | 3
5 | 4
6 | 2
I want the result to be:
TABLE
A
-
1
2
3
5
4
6
Use the UNION operator
SELECT A FROM TABLE
UNION
SELECT B FROM TABLE