one to many mapping without any relation in tables mysql - mysql

i have two tables named as oc_users and oc_groups there is no specific relation between both the tables as shown below so, here i want to map each user with each group:
1)table 1:
select uid from oc_users;
+-----------------+
| uid |
+-----------------+
| manesh#abc.in |
| pankaj |
| sumit |
+-----------------+
2)table 2:
select gid from oc_groups;
+---------+
| gid |
+---------+
| qlc |
| qlc-web |
+---------+
Then i want o/p like:
+---------+-----------------+
| gid | uid |
+---------+-----------------+
| qlc | manesh#abc.in |
| qlc | pankaj |
| qlc | sumit |
| qlc-web | manesh#abc.in |
| qlc-web | pankaj |
| qlc-web | sumit |
+---------+-----------------+

Use this
select * from oc_user , oc_groups ORDER BY gid, uidS
This will print all columns with Cartesian multiplecation of rows.

you need to use CROSS JOIN (new SQL Syntax: ANSI SQL-92)
SELECT gid, uid
FROM oc_users CROSS JOIN oc_groups
ORDER BY gid, uid
SQLFiddle Demo

Using CROSS JOIN(Cartesian Product Join) will solve your purpose. For your information
A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table.
So simply this will do,
SELECT gid, uid
FROM oc_users CROSS JOIN oc_groups;
Always go for 'CROSS JOIN' syntax instead of giving nothing. Since this is ANSI format you can use it across database and it will be useful during migration also as you dont need to change anything in your query.
Hope this helps you!!

Related

Select count with value from different tables

I want to count all entries in one table grouped by the user id.
This is the query I used which works fine.
select uuid_mapping_id, count(*) from t_message group by uuid_mapping_id;
and these are the results:
+-----------------+----------+
| uuid_mapping_id | count(*) |
+-----------------+----------+
| 1 | 65 |
| 4 | 277 |
Now I would like to display the actual user name, instead of the ID.
To achieve this I would need the help of two different tables.
The table t_uuid_mapping which has two columns:
uid_mapping_id, which equals uuid_mapping_id in the other table.
And f_uuid which is also unique but completely different.
f_uuid can also be found in another table t_abook which also contains the names in the column f_name.
The result I am looking for should be:
+-----------------+----------+
| f_name | count(*) |
+-----------------+----------+
| admin | 65 |
| user1 | 277 |
I am new to the database topic and understand that this could be achieved by using JOIN in the query, but to be honest I did not completely understand this yet.
if I understand you correctly:
SELECT tm.f_name, COUNT(*) as count
FROM t_message tm
LEFT JOIN t_abook ta ON (tm.uuid_mapping_id = ta.uid_mapping_id)
GROUP BY tm.f_name

Inserting data from a join within tables

I'm trying to figure out how to extend a table of company_ids using a group reference from another table. For a given company_name, I want to get its company_ids, join them with the company_groups table, and get their group_ids. From there, I want to get every company_id associated with the group_id, and insert them in the companies table with the proper company_name.
The structure is a bit atypical. A single company name can have many IDs, because the ID isn't used as a unique identifier for a single company. I'm trying to make sure all of them get copied to the companies table. I'm sure there's a fairly simple join command, but I've never been very good at joining tables within themselves. Can anyone help? Thanks!
+---------------+
| companies |
+---------------+
| company_id PK |
| company_name |
+---------------+
+----------------+
| company_groups |
+----------------+
| company_id PK |
| group_id |
+----------------+
Here's an example. In the companies table, I have the company "Tesla", with three different IDs.
+--------------+------------+
| company_name | company_id |
+--------------+------------+
| Tesla | 647552 |
| Tesla | 927572 |
| Tesla | 748563 |
+--------------+------------+
In the company_groups table, each group_id has one or more company_ids.
+----------+------------+
| group_id | company_id |
+----------+------------+
| 227 | 647552 |
| 227 | 111743 |
| 227 | 111842 |
+----------+------------+
I want to match the company_name to multiple group_ids, then find all company_ids associated with the group. The new company_ids would then be inserted in the companies table like this:
+--------------+------------+
| company_name | company_id |
+--------------+------------+
| Tesla | 647552 |
| Tesla | 927572 |
| Tesla | 748563 |
| Tesla | 111743 |
| Tesla | 111842 |
+--------------+------------+
You cannot insert into a table when the same table name appear in the where clause.
You can use a temporary third table to do this job
At second thought, your question is either funny or not clear enough, please provide your sample data, so that i can update my answer
I figured it out. A nested query works well:
REPLACE INTO companies (company_name, company_id)
SELECT a.company_name, company_groups.company_id
FROM
(
SELECT group_id, company_groups.company_id, companies.company_name
FROM companies
JOIN company_groups
ON companies.company_id=company_groups.company_id
GROUP BY group_id
) a
JOIN company_groups
ON company_groups.group_id=a.group_id
GROUP BY company_id;

Mysql: how to return value or empty string from column ids in table (multi table)

I'M trying to extract all information into my table, but I need to change id, when available, to the name into another table.
I have 1 table like that:
|------------------------------|
|-id-|-systems-|-remote-|-deco-|
| 1 | NULL | 3 | |
| 2 | 21 | NULL | 2 |
|-------------------------------
each column like "systems" / "remote" / "deco" refer to an id into another table
I know how to use INNER JOIN. But if I use that, I got an empty result because the value need to be appears into the others tables.
ex.:
SELECT qd.id,s.name as systems,r.name as remote, d.name as deco
FROM `quote_data` qd
INNER JOIN systems s ON qd.systems=s.id
INNER JOIN remote r ON qd.remote=r.id
INNER JOIN deco d ON qd.deco=d.id
I got empty result.
In the best words, I need to do something like:
|------------------------------|
|-id-|-systems-|-remote-|-deco-|
| 1 | | R42 | |
| 2 | GTV | | B21 |
|-------------------------------
Also, I use innoDB table
Any Idea how to fix that?

Concatenate fields of rows with the same ID in MySQL

I have the following query:
SELECT mutations.id, genes.loc FROM mutations, genes where mutations.id=genes.id;
and outputs this:
| SL2.50ch02_51014904 | intergenic |
| SL2.50ch02_51014907 | upstream |
| SL2.50ch02_51014907 | downstream |
| SL2.50ch02_51014907 | intergenic |
| SL2.50ch02_51014911 | upstream |
| SL2.50ch02_51014911 | downstream |
My desired output is this:
| SL2.50ch02_51014904 | intergenic |
| SL2.50ch02_51014907 | upstream,downstream,intergenic |
| SL2.50ch02_51014911 | upstream,downstream |
I thought GROUP_CONCAT was useful for this. However, doing this:
SELECT mutations.id, GROUP_CONCAT(distinct(genes.loc)) FROM mutations, genes WHERE mutations.id=genes.id;
I have a unique row like this:
SL2.50ch02_51014904 | downstream,intergenic,upstream
How can I solve this?
You need to add group by:
SELECT m.id, GROUP_CONCAT(distinct(g.loc))
FROM mutations m JOIN
genes g
ON m.id = g.id
GROUP BY m.id;
Along the way, you should learn a couple other things:
Use explicit join syntax. A simple rule: never use commas in the from clause.
Use table aliases (the m and g). They make the query easier to write and to read.
You forgot the GROUP BY:
SELECT
mutations.id,
GROUP_CONCAT(DISTINCT(genes.loc))
FROM
mutations, genes
WHERE
mutations.id=genes.id
GROUP BY
mutations.id

MySQL: Matching records from 2 tables with multiple values

I have 2 tables:
table1
id | title | author | url
1 | the-test | james-brown | www.thetest.com
2 | the-house | clancy-brown | www.thehouse.com
3 | the-desk | leanne-brown | www.thedesk.com
4 | the-head | julie-brown | www.thehead.com
table2
id | title | author | url**
1 | the-mouse | john-blue | www.themouse.com
2 | the-house | clancy-brown | www.thehouse.com
3 | the-cups | carrie-blue | www.thecups.com
4 | the-head | clancy-brown | www.thehead.com
I need results to show only where both title and author match, i.e.
2 | the-house | clancy-brown | www.thehouse.com
I've tried this:
select *
from table1
inner join table2 on (table1.title=table2.title) AND (table1.author=table2.author)
But it just runs forever (there are actually several hundred thousands rows in the real tables).
Just to note, this works fine:
select *
from table1
inner join table2 on (table1.title=table2.title)
I just can't get it to match both tables. Is there a smarter way to do this?
Thanks for all help in advance.
I think your SQL is fine, you just need to index table1 and table2 on author. Add those indices and the JOIN will be fast enough. You also have an issue that you call the column author in your sample data and name in your first query.