Select from MySQL table while ordering by IDs from another table - mysql

This might be something very simple to do. If so, I apologize. I'm still learning MySQL.
Say, I have two tables:
Table1:
`id` int autoincrement primary key
`Name` tinytext
`Phone` tinytext
`Date` etc.
and
Table2:
`id` int autoincrement primary key
`itmID` int
Each row in Table2 specifes the order at which elements should be selected out of Table1. itmID field in Table2 is linked to id field in Table1.
So right at this moment to select elements from Table1 I do this:
SELECT * FROM `Table1`;
But how do you order them according to Table2, something like this?
SELECT * FROM `Table1` ORDER BY <itmID's in Table2> ASC;

If all ids of the Table1 have an entry on Table2 use an INNER JOIN, like this.
SELECT * FROM Table1 t1
INNER JOIN Table2 t2 ON t1.id = t2.itmID
ORDER BY t2.itmID
If not all of them have an entry, then use a LEFT JOIN, like this:
SELECT * FROM Table1 t1
LEFT JOIN Table2 t2 ON t1.id = t2.itmID
ORDER BY t2.itmID

Select from the first table, join it to the second, and order by the second. Something like
SELECT *
FROM table1
LEFT JOIN table 2 on table.id = table2.id
ORDER by table2.itmID

Ryan's answer is almost right
SELECT *
FROM table1
INNER JOIN table2 on table1.id = table2.itmID
ORDER BY table2.id

http://dev.mysql.com/doc/refman/5.5/en/join.html
SELECT * FROM `Table1`
INNER JOIN `Table2` USING (`id`)
ORDER BY `Table2`.`itmID` ASC

Related

Can I add a new auto-increment column to this UNION?

I’m trying to join two tables getting all the data from both tables.
I managed to UNION both tables, but I need to add an ID with auto-increment as the primary key for the new table that I’m creating.
I don't know how to do it and can’t find a way to add it to the query.
CREATE TABLE NEWTABLE
SELECT T1.TEXT as TEXT
[...]
FROM TABLE1 T1
LEFT JOIN TABLE2 T2
on T1.TEXT = T2.TEXT
UNION
SELECT T2.TEXT as TEXT
FROM TABLE1 T1
[...]
RIGHT JOIN TABLE2 T2
on T1.TEXT = T2.TEXT
You need to put the column definitions in the CREATE TABLE statement to add the id column. Then provide NULL as the values for it in the SELECT queries.
CREATE TABLE NEWTABLE (
id INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
text TEXT,
[...]
) AS
SELECT null, T1.TEXT
[...]
FROM TABLE1 T1
LEFT JOIN TABLE2 T2
on T1.TEXT = T2.TEXT
UNION
SELECT null, T2.TEXT as TEXT
FROM TABLE1 T1
[...]
RIGHT JOIN TABLE2 T2
on T1.TEXT = T2.TEXT

SQL Select on multiple tables to check if value exists in one table and not used in other

I have 2 tables:
table1 (id,usedcode)
table2 (codeid,uniquecode)
I want to be able to check if a certain value exists in uniquecode of Table2, but is not already used in Table1
Try using left join as below:
SELECT t2.*
FROM table2 t2 LEFT JOIN table1 t1
ON t2.uniquecode = t1.usedcode
WHERE t1.usedcode IS null
SELECT uniquecode FROM Table2
WHERE NOT EXISTS(
SELECT * FROM Table1 WHERE usedcode = uniquecode
)
In English the query is saying, "Select all unique codes from table 2 that don't exist in table 1 as a usedcode".

select from table based on select distinct from another table

the case is that I need to select a field distinct from table1 (no duplicates) and use the result as a key to select from another table2. And I need this to be in one query. Is this possible?!
table1: hID, hName, hLocation
table2: hID, hFrom, hTo, hRate, hRoomType, hMeals
I want to correct version of this query:
SELECT
*
FROM
table1
JOIN (
DISTINCT
hID
FROM
table2
WHERE
hRoomType = Double Room
ON table1.hID = table2.hID)
expected result: all hotels that offer Double Room thanks much –
thanks for help!
Your question is quite vague and confusing. Is this what you are looking for:
SELECT hID, name, location
FROM table2
INNER JOIN table1
ON table1.hID = table2.hID
GROUP BY table2.hID;
Here is a skeleton to achieve this:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM (
-- This is the distinct list from table1
SELECT DISTINCT
id
FROM
table1 T1
) DT1
INNER JOIN table2 T2
ON T1.id = T2.reference_to_t1_id
Another solution if you don't want to retrieve any columns from table1:
SELECT
* -- Don't forget to list the requested fields instead of using `*`!
FROM
table2 T2
WHERE
-- Sais that get all record from table2 where this condition matches
-- at least one record
EXISTS (
SELECT 1 FROM table1 T1 WHERE T1.id = T2.reference_to_t1_id
)
For your tables and question
SELECT
hID, hName, hLocation
FROM
table1 T1
WHERE
EXISTS (
SELECT 1 FROM
table2 T2
WHERE
T1.hID = T2.hID
AND T.hRoomType = 'Double' -- Assuming that this is the definition of double rooms
)

Joining tables with Foreign Keys

How do I INNER JOIN a table that contains 2 foreign keys as its primary keys?
CREATE TABLE table1 (table1ID CHAR(4));
CREATE TABLE MEM_INSTR (table2ID CHAR(4));
CREATE TABLE table3 (table1ID CHAR(4), table2ID CHAR(4));
Assuming you want to just join everything together as keys suggest...
SELECT *
FROM table1
INNER JOIN table3 on table3.table1ID = table1.table1ID
INNER JOIN MEM_INSTR on MEM_INSTR.table2ID = table3.table2ID
But let's say that you have this scenario.
CREATE TABLE Table1 (
Table1ID NUMBER,
Generation NUMBER,
...
);
CREATE TABLE Table2 (
Table2ID NUMBER,
Table1ID NUMBER,
Table1Generation NUMBER,
...
);
Let's say for argument's sake that Table1 can have multiple records with the same Table1ID, and Generation is used as a secondary key. And you need to join a Table2 record to the correct single Table1 record. You can expand the ON clause the same way you would expand a WHERE clause.
SELECT *
FROM table1 t1
INNER JOIN table2 t2
ON t2.table1id = t1.table1id
AND t2.table1generation = t1.generation
You join it like you usually do, nothing really special about that. So you go something like this:
SELECT ...
FROM table1
INNER JOIN table3 ON table3.table1ID = table1.table1ID
INNER JOIN MEM_INSTR ON MEM_INSTR.table2ID = table3.table2ID

query data from one table that matches with value from another table

I have two table created as
create table table1(id int,);
create table table2(id int, tb2_id int,...)
But when i try out
Select * from table2 where tb2_id=table1.id;
I have got an error that table1.id is an unknown column.
Could someone point out where the mistake I made is ?
You probably want to JOIN tables:
SELECT table2.* FROM table2 JOIN table1 ON (table2.tb2_id=table1.id)
Select * from table2, table1 where tb2_id=table1.id;
You need either a join or a subquery.
Select t2.*
from table2 t2
Inner join table1 t1
On t2.tbl2_id = t1.id
Or
Select t2.*
from table2 t2
where tbl2_id in ( select id from table1 )
try this:
SELECT *
FROM Table2
WHERE ID IN (SELECT ID FROM Table1)