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
Related
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
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?
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
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
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;