MySQL: Using a reference id, see if data matches across that id - mysql

I have the following table
id | data | reference_id
1 Jacob 3
2 Apples 3
3 Henry 4
4 Pie 4
5 Jacob 5
6 Pears 5
What I'm trying to do is build a query that checks if data exists in the same reference_id given the data.
So basically:
SELECT COUNT(*) WHERE data='Jacob' AND data='Apples' AND
...(reference_id's are the same)

SELECT COUNT(*) WHERE data='Jacob' AND data='Apples' GROUP BY reference_id;

Related

Mysql Joint Two district column into one [duplicate]

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

Fetching crossed records from the database

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?

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

Select only ids from table A that don't have multiple records

table a
id | food
1 | rice
2 | rice
3 | rice
4 | rice
1 | beans
My results should only be ids (2 ,3 and 4) with the food type. I don't want id 1 because it has 2 different records. Help?
Well,
Assuming that your expected output is:
3,4 -> you could try this:
SELECT id
FROM tableA
GROUP BY id
HAVING COUNT(id) = 1
This will select all ids from the table that occur only once

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