Mysql query of multiple tables joined - mysql

I have the following tables:
machine_machine
id | machineid
1 | EE100034442
item_item
id | upc | name
2 | 10001 | Snickers
machine_setup
id | machine_id | selection | item_id
3 | 1 | A1 | 1
Im trying to get the following output by joining the tables.
machine_setup.machine_id=machine_machine.machineid, machine_setup.selection, item_item.upc, item_item.name
EE100034442 A1 10001 Snickers
Table machine_setup will by the main referenced table as it has multiple selection for each machine_id.

Based on the only id's I can see at the moment to join on, consider this:
create table machine_machine
( id int auto_increment primary key,
machineid varchar(50) not null
);
create table item_item
( id int auto_increment primary key,
upc varchar(30) not null,
name varchar(100) not null
);
create table machine_setup
( id int auto_increment primary key,
machine_id int not null,
selection varchar(30) not null
);
insert machine_machine(machineid) values ('EE100034442');
insert item_item(upc,name) values ('10001','Snickers');
insert machine_setup(machine_id,selection) values (1,'A1'),(1,'A2'),(1,'A(n)');
select mm.machineid,ms.selection,ii.upc,ii.name
from machine_setup ms
join machine_machine mm
on mm.id=ms.machine_id
join item_item ii
on ii.id=ms.machine_id;
+-------------+-----------+-------+----------+
| machineid | selection | upc | name |
+-------------+-----------+-------+----------+
| EE100034442 | A1 | 10001 | Snickers |
| EE100034442 | A2 | 10001 | Snickers |
| EE100034442 | A(n) | 10001 | Snickers |
+-------------+-----------+-------+----------+

I'm not quite sure I understand the question, but the sql you want is like;
Select machine1.machineid, setup.Selection, item.upc, item.name
From Machine_machine machine1 --Set alias for the table
Inner Join machine_setup setup on setup.machine_id = machine1.id --This looks like a link table to me
Inner Join item_item item on setup.item_id = item.id -- in your example this wouldn't link as item_id is 1 in the machine_setup
In your example the machine_setup item_id is set to 1, which means it wouldn't link to the item_item table. i'm assuming this is a mistake.
Let me know if you need more information.

Related

Combine two tables with same Primary key

This seems like a simple question but I can't seem to find an answer. I have two tables. Table 1:
+---------+-----------+--------+-------+----------+
| userid | username | date | time | footsize |
+---------+-----------+--------+-------+----------+
| 1 | user1 | 103999 | 1010 | 9 |
| 2 | user2 | 484883 | 984 | 6 |
+---------+-----------+--------+-------+----------+
and Table 2:
+---------+-----------+----------+
| userid | natural | synthetic|
+---------+-----------+----------+
| 1 | y | n |
| 2 | n | y |
+---------+-----------+----------+
What I'd like to do is delete table 2.
But I need to move the columns and data natural and synthetic from table 2 and insert them into table 1, using userid as a primary key to make sure the data goes to the right customer.
I tried using the join statements but I can't seem to move them from joining to inserting without an error.
The general (loose) idea I want is
select userid from table1, select * from table2.
Insert into table1, table2.natural, table2.synthetic where table1.userid = table2.userid;
So that table 1 looks like this:
+---------+-----------+--------+-------+----------+-----------+----------+
| userid | username | date | time | footsize | natural | synthetic|
+---------+-----------+--------+-------+----------+-----------+----------+
| 1 | user1 | 103999 | 1010 | 9 | y | n |
| 2 | user2 | 484883 | 984 | 6 | n | y |
+---------+-----------+--------+-------+----------+-----------+----------+
I'm aware that's not a real query, but it should clarify what I'm trying to do. Thanks!
first you create a table that has all the columns
CREATE TABLE `table3` (
`userid` INT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(45) NULL,
`date` INT NULL,
`time` INT NULL,
`footsize` INT NULL,
`natural` VARCHAR(45) NULL,
`synthetic` VARCHAR(45) NULL,
PRIMARY KEY (`userid`));
insert into table3
select t1.userid, t1.username, t1.`date`, t1.`time`, t1.footsize,
t2.natural, t2.synthetic
from table1 as t1
join table2 as t2
on t1.userid = t2.userid

Mysql - Select, store in var, select in same query

I am trying to understand how to do this. Not much help found.
I have two tables.
First table:
CREATE TABLE `first` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`parent_id` int(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
)
Second table:
CREATE TABLE `second` (
`id` int(11) NOT NULL,
`foo` varchar(3) NOT NULL ,
`bar` varchar(255) NOT NULL )
Id in both tables is same value, they are related that way.
I have to fetch some entries, for example with parent_id = '0' and to check if those entries have children. If they has I should get their ids too.
It could be done easily with little php and two queries but I would like to do it better. I tried something like:
SELECT `first`.*, `second`.*, children.*
FROM `first`
JOIN `second` ON `second`.id = `first`.id
INNER JOIN( SELECT
// Something
)AS children ON children.id = `second`.id
WHERE `first`.parent_id = '0'
GROUP BY `first`.id, `second`.la
But I did not make it.
How could I Select those result and their children in same query? Thanks!
EDIT
First:
-------------------------------------------------
| id | parent_id |
--------------------------------------------------
| 1 | 0 |
--------------------------------------------------
| 2 | 1 |
--------------------------------------------------
| 3 | 2 |
--------------------------------------------------
| 4 | 1 |
--------------------------------------------------
Second table:
-------------------------------------------------
| id | foo | name
--------------------------------------------------
| 1 | asd | apple
--------------------------------------------------
| 2 | asd | banana
--------------------------------------------------
| 3 | gsf | orange
--------------------------------------------------
| 4 | gre | potato
--------------------------------------------------
Desired Result would be to get row where id is 1 (joined both tables) and all ids of rows witch have parent_id 1.
Thanks!

MySQL indexing to efficiently query table join with many to many relationship

I've been struggling to create the correct index to improve performance for a specific query we run on our database, I hope one of you can help point me in the right direction.
I have 2 tables as per below. I have a query to find all employees of companies that belong to a specific category with first_name like "Be%" for example. I've tried creating a multiple index on category_id, company_id but this hasn't helped. What should be the correct way to index my tables to achieve better performance for this query? Thank you in advance.
SELECT
e.*
FROM employee e
INNER JOIN company c ON e.company_id = c.company_id
WHERE c.category_id = 6
AND e.first_name LIKE "Be%"
GROUP BY e.employee_id
TABLE company
| company_id | category_id |
+------------+-------------+
| ... | ... |
+------------+-------------+
| 47 | 6 |
+------------+-------------+
| .. | ... |
+------------+-------------+
| 252 | 6 |
+------------+-------------+
TABLE employee
| employee_id | company_id | first_name | ... |
+-------------+------------+------------+-----+
| 2582250 | 47 | Ben | ... |
+-------------+------------+------------+-----+
| 3447890 | 252 | Ryan | ... |
+-------------+------------+------------+-----+
| 7125966 | 252 | Beth | ... |
+-------------+------------+------------+-----+
| ... | ... | ... | ... |
+-------------+------------+------------+-----+
CREATE TABLES below and sqlfiddle.
CREATE TABLE company (
`company_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`category_id` INT UNSIGNED NOT NULL DEFAULT 0
) ENGINE=InnoDB;
CREATE TABLE employee (
`employee_id` INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
`company_id` INT UNSIGNED NOT NULL,
`first_name` VARCHAR(255)
) ENGINE=InnoDB;
You must have indexes in many:many tables. Follow the advice outlined here.
WHERE c.category_id = 6
AND e.first_name LIKE "Be%"
needs
c: INDEX(category_id, company_id) -- which will be called for in the link above
e: INDEX(first_name, employee_id)
The Optimizer will either start with c, then reach into e. Or vice versa. I am providing you the optimal indexes for either direction.

Need help in merging three tables foreign keys

I have created three tables like this,
1.
CREATE TABLE person (
id int NOT NULL AUTO_INCREMENT,
name varchar(50) NOT NULL,
age int,
PRIMARY KEY (id)
);
2.
CREATE TABLE address (
id int NOT NULL AUTO_INCREMENT,
city varchar(50) NOT NULL,
post_code int NOT NULL,
person_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (person_id) REFERENCES person(id)
);
3
CREATE TABLE subjects (
id int NOT NULL AUTO_INCREMENT,
subjects_s varchar(50) NOT NULL,
address_id int NOT NULL,
PRIMARY KEY (id),
FOREIGN KEY (address_id) REFERENCES address(id)
);
Now in tables i have some informations like this:
person
+----+--------+------+
| id | name | age |
+----+--------+------+
| 1 | Sohan | 17 |
| 2 | Farhan | 18 |
+----+--------+------+
address
+----+-------+-----------+-----------+
| id | city | post_code | person_id |
+----+-------+-----------+-----------+
| 1 | Tongi | 1711 | 1 |
| 2 | Dhaka | 1230 | 2 |
+----+-------+-----------+-----------+
subjects
+----+--------------------+------------+
| id | subjects_s | address_id |
+----+--------------------+------------+
| 1 | Accounting Finance | 1 |
| 2 | Physics Math | 2 |
+----+--------------------+------------+
Now I want to show all these data together. How can I do this? Please help!
You should be able to use a SQL join statement to combine these.
The syntax is detailed in the MySQL join documentation.
With your tables, your query should look something like this:
SELECT person.*, address.*, subjects.*
FROM person
JOIN address ON person.id = address.person_id
JOIN subjects ON address.id = subjects.address_id
Keep in mind, this example uses an inner join, which may not be the right type of join depending on the data in your tables. I'd recommend reading the documentation I linked above for further guidance.

MySQL Sort By Column, Then Pull The Rest

I have a unique situation where I need to pull many records, but sort SOME at the top if they are considered "favorites"...but then I need to pull all the rest of the records below this group.
Can this be done in one query...or will I need two? Any examples?
You can have a custom Order By with a CASE/WHEN clause:
Schema:
create table music
( mId int auto_increment primary key,
song varchar(100) not null
);
create table person
( pId int auto_increment primary key,
pName varchar(100) not null
);
create table person_owns_music
( id int auto_increment primary key,
mId int not null,
pId int not null,
favorite int not null,
rating int not null, -- 100 loves it. 0 hates it
foreign key `pom_2_music` (mId) references music(mId),
foreign key `pom_2_person` (pId) references person(pId)
);
-- truncate table person_owns_music;
insert music(song) values ('s1'),('s2'),('s3'),('s4'),('s5'),('s6');
insert person(pName) values ('p1'),('p2'),('p3');
insert person_owns_music(mId,pId,favorite,rating) values
(1,1,1,10),(2,1,1,100),(3,1,0,65),(4,1,1,15),(6,1,1,5),
(1,2,1,10),(2,2,1,100),(5,2,1,100);
Query:
SELECT pom.mId,m.song,pom.favorite,pom.rating
FROM person_owns_music pom
JOIN music m ON m.mId=pom.mId
WHERE pom.pId=1
ORDER BY CASE pom.favorite
WHEN 1 THEN 1
WHEN 0 THEN 2
END, pom.rating DESC;
+-----+------+----------+--------+
| mId | song | favorite | rating |
+-----+------+----------+--------+
| 2 | s2 | 1 | 100 |
| 4 | s4 | 1 | 15 |
| 1 | s1 | 1 | 10 |
| 6 | s6 | 1 | 5 |
| 3 | s3 | 0 | 65 |
+-----+------+----------+--------+