I've three table 'Hardware_model','Warehouse' and 'Brand' and tables are refernced together in this way:
Hardware_model <-> Warehouse
Hardware_model <-> Brand
Now I want execute the following query: select all 'warehouse' object that has a 'brand_id equal to '10'). Off course warehouse and brand are not joined so no foreign keys exists between them. I was trying something like:
SELECT *
FROM warehouses
where hardware_id = (SELECT *
FROM hardwares
where brand_id='10')
but it doesn't works!
where hardware_id is a 'warehouse table' field and brand_id is a 'hardware table' field.
Any suggestions?
This sounds like a simple multi-table join. You just need to do something along the lines of (I can only guess the table structure).
SELECT w.* FROM warehouses w
JOIN hardwares h ON w.hardware_id = h.hardware_id
JOIN brands b ON h.brand_id = b.brand_id
WHERE brand_id=10;
It doesn't work because your subquery returns more than one column, and possibly more than one row. You should be able to do this with a JOIN:
SELECT *
FROM warehouses
JOIN hardwares ON warehouses.hardware_id = hardwares.id
WHERE brand_id = '10'
hardwares.id should be replaced with whatever key warehouses.hardware_id references. Even if you haven't specified a FOREIGN KEY constraint, you can still join tables.
just guessing at your tables, but maybe
SELECT w1.*
FROM warehouses w1,
hardware h1
where w1.hardware_id = h1.hardware_id
and h1.brand_id=10
Do you have a hardware_id in your warehouse table? if not, where are you making the relation between your warehouse and what is in it?
Surpriased that doesn't fall over in a big heap should be something like
SELECT * FROM warehouses
where hardware_id in (SELECT hardware_id FROM hardwares where brand_id='10')
Related
I have a table, System, with a bunch of fields including System.serial.
I have a list of serial numbers that I want to get the status of.
Simple enough:
Select * from System where System.serial in ('s1','s2', 'sn');
However the list of serial numbers also has serials NOT IN the System table.
Obviously they are not in the results.
I want the missing serials to show in the results also but with no data.
The best way I can think of doing this is to make a temporary table with one column, serial, and then left join System on it.
How can I do this without creating a temporary table?
Something like:
Select listOfSerials.serial, System.*
from (Select ('s1','s2', 'sn') as serial ) as ListOfSerials
left join System on System.serial = ListOfSerials.serial;
Thanks,
Ryan
You're on the right track with your solution of creating a virtual table with which to do LEFT JOIN against your real data.
You can create a derived table as a series of UNIONed SELECT statements that select literal values with no table reference.
SELECT listOfSerials.serial, System.*
FROM (
SELECT 's1' AS serial
UNION SELECT 's2'
UNION SELECT 'sn'
) AS ListOfSerials
LEFT JOIN System ON System.serial = ListOfSerials.serial;
You only need to define a column alias in the first SELECT in the UNION. The rest are required to use that column alias.
Creating a reference table to store the list of serials is probably your best option. That would allow you to write a query like:
SELECT r.serial reference_serial, s.serial system_serial
FROM reference_table r
LEFT JOIN system_table s ON s.serial = r.serial
With the LEFT JOIN, serials declared in the reference table but unavailable in the system table will have second column set to NULL.
A quick and dirty work around is to use UNIONed subqueries to emulate the reference table:
SELECT r.serial reference_serial, s.serial system_serial
FROM (
SELECT 'serial1' AS serial
UNION ALL SELECT 'serial2'
UNION ALL SELECT 'serial2'
...
) r
LEFT JOIN system_table s ON s.serial = r.serial
Hi im trying to make a left join on this database.
Table category:
// `category` table has 3 columns
id,business_id (FK from business),category.
Table business:
// `business` table has 12 columns
id, name, etc (all info for that business)
What I need is to filter all the business that are Restaurants and join it to the respective business ID.
Problem is that each business have multiple category and when I do a select/join the result doesnt return DISTINCT businesses.
Here is one of the query i tried:
SELECT category.category,business.*
FROM category
INNER JOIN business
ON category.business_id = business.id;
I also tried left, right joins. But my pc is either taking forever or not working.
P.S the dataset is 8.6G
![database output]: https://i.imgur.com/eF4zYOr.png
Your query looks okay, but it is lacking that you are only looking for restaurants. However, firstly, I would make sure I have an index built on category.id
create index your_index_name on your_table_name(your_column_name);
Then you can simplify your query this way:
select a.id, a.category, b.* from category a left join business b on a.business_id=b.id WHERE a.category='Restaurants';
Even with that, a table that's 8.6G is going to take time. Since you said "pc" instead of "server" it might take a long time.
Try this
SELECT *
FROM Business b
WHERE EXISTS (
SELECT 1
FROM Category
WHERE category = 'Restaurants' AND business_id = b.id
)
My table has a columns labeled primary_key and summary_id. The value in the second field summary_id in each record maps to the primary_key field of another record. There is a third field template_id. I need to select those records for which:
template_id is a certain value. Let's say 4.
primary_key matches at least one of the records' summary_id field.
Please don't tell me to redesign the tables. My next project will have a better design, but I don't have time for that now. I need to do this with one or more queries; the fewer the better. Ideally, there's some way to do this with one query, but I'm okay if it requires more.
This is how far I've gotten with my own query. (I know it's seriously lacking, which is why I need help.)
SELECT DISTINCT esjp_content.template_id
FROM esjp_content
INNER JOIN esjp_hw_config ON esjp_content.template_id = esjp_hw_config.proc_id
INNER JOIN esjp_assets ON esjp_hw_config.primary_key = esjp_assets.hw_config_id
WHERE
esjp_content.summary_id > 0
AND
(esjp_assets.asset_label='C001498500' OR esjp_assets.asset_label='H0065' OR esjp_assets.asset_label='L0009');
SELECT
esjp_content.primary_key, esjp_content.template_id, esjp_content.content, esjp_content.summary_id
FROM
esjp_content
WHERE
esjp_content.template_id = 4;
I need the records that summary_id points to. For example, if summary_id is 90, then I need the record where primary_key is 90.
You're looking for the existence of at least one row where summary_id = your primary key. like this.
SELECT *
FROM esjp_content c
WHERE template_id = 4
AND EXISTS (SELECT 1 FROM esjp_content c2 WHERE c2.summary_id = c.primary_key)
You can JOIN same table by using both IDs:
SELECT
t1.*
FROM
esjp_content t1
INNER JOIN esjp_content t2 ON t1.summary_id = t2.primary_key
WHERE
t1.template_id = 4
I have 2 Tables in phpmyadmin that need joining
tracklisting is my one and catelogue is the other, and are saved as innodb
They both have a column CAT.NO and would like it to be joined on this column. In catelogue it is the primary and in tracklisting it's indexed
catelogue is my parent and tracklisting would be the child as it doesn't have info for every record in catelogue. I believe this would be correct unless I'm wrong
How do I do this so that when I query on a column in tracklisting it only brings up the matches for 'catelogue' because I want to know what album it's on and not my entire 60000+ catelogue
Can this be done with phpmyadmin's interface or is this a sql statement
Many thanks
EDIT:
This was the code that worked
SELECT *
FROM tracklisting
INNER JOIN catelogue ON catelogue.`CAT NO.` = tracklisting.`TRACKLISTING CAT NO.`
WHERE tracklisting.`ARTIST` LIKE 'placebo'
Thanks to everyone that helped out
I dont know if this can be done with the interface, but with sql
SELECT *
FROM
tracklisting t
INNER JOIN catelouge c on c.catno=t.catno
WHERE t.id = 1
You can query with a LEFT JOIN:
SELECT * FROM tracklisting LEFT JOIN catelogue ON tracklisting.`CAT.NO` = catelogue.`CAT.NO` WHERE tracklisting.`<id-field>` = <id-value>`
http://dev.mysql.com/doc/refman/5.0/en/join.html
Assuming that your tracklisting has an id and you want to query with it:
select * from tracklisting t
inner join catelogue c on c.cat.no = t.cat.no
where t.id = 1
The actual join takes place in a SQL statement although with certain storage types like InnoDB you can also create foreign key references that enforce the relationship at the database level so that your inserts require the proper records to be in place and your deletes are restricted if child records exist.
Here is one way of doing the join syntax for your SQL query:
SELECT t.* FROM tracklisting t, catalogue c
WHERE `t.CAT.NO` = `c.CAT.NO`
EDIT: Here is a link to a tutorial for creating foreign keys in phpMyAdmin.
I need to gather posts from two mysql tables that have different columns and provide a WHERE clause to each set of tables. I appreciate the help, thanks in advance.
This is what I have tried...
SELECT
blabbing.id,
blabbing.mem_id,
blabbing.the_blab,
blabbing.blab_date,
blabbing.blab_type,
blabbing.device,
blabbing.fromid,
team_blabbing.team_id
FROM
blabbing
LEFT OUTER JOIN
team_blabbing
ON team_blabbing.id = blabbing.id
WHERE
team_id IN ($team_array) ||
mem_id='$id' ||
fromid='$logOptions_id'
ORDER BY
blab_date DESC
LIMIT 20
I know that this is messy, but i'll admit, I am no mysql veteran. I'm a beginner at best... Any suggestions?
You could put the where-clauses in subqueries:
select
*
from
(select * from ... where ...) as alias1 -- this is a subquery
left outer join
(select * from ... where ...) as alias2 -- this is also a subquery
on
....
order by
....
Note that you can't use subqueries like this in a view definition.
You could also combine the where-clauses, as in your example. Use table aliases to distinguish between columns of different tables (it's a good idea to use aliases even when you don't have to, just because it makes things easier to read). Example:
select
*
from
<table> as alias1
left outer join
<othertable> as alias2
on
....
where
alias1.id = ... and alias2.id = ... -- aliases distinguish between ids!!
order by
....
Two suggestions for you since a relative newbie in SQL. Use "aliases" for your tables to help reduce SuperLongTableNameReferencesForColumns, and always qualify the column names in a query. It can help your life go easier, and anyone AFTER you to better know which columns come from what table, especially if same column name in different tables. Prevents ambiguity in the query. Your left join, I think, from the sample, may be ambigous, but confirm the join of B.ID to TB.ID? Typically a "Team_ID" would appear once in a teams table, and each blabbing entry could have the "Team_ID" that such posting was from, in addition to its OWN "ID" for the blabbing table's unique key indicator.
SELECT
B.id,
B.mem_id,
B.the_blab,
B.blab_date,
B.blab_type,
B.device,
B.fromid,
TB.team_id
FROM
blabbing B
LEFT JOIN team_blabbing TB
ON B.ID = TB.ID
WHERE
TB.Team_ID IN ( you can't do a direct $team_array here )
OR B.mem_id = SomeParameter
OR b.FromID = AnotherParameter
ORDER BY
B.blab_date DESC
LIMIT 20
Where you were trying the $team_array, you would have to build out the full list as expected, such as
TB.Team_ID IN ( 1, 4, 18, 23, 58 )
Also, not logical "||" or, but SQL "OR"
EDIT -- per your comment
This could be done in a variety of ways, such as dynamic SQL building and executing, calling multiple times, once for each ID and merging the results, or additionally, by doing a join to yet another temp table that gets cleaned out say... daily.
If you have another table such as "TeamJoins", and it has say... 3 columns: a date, a sessionid and team_id, you could daily purge anything from a day old of queries, and/or keep clearing each time a new query by the same session ID (as it appears coming from PHP). Have two indexes, one on the date (to simplify any daily purging), and second on (sessionID, team_id) for the join.
Then, loop through to do inserts into the "TempJoins" table with the simple elements identified.
THEN, instead of a hard-coded list IN, you could change that part to
...
FROM
blabbing B
LEFT JOIN team_blabbing TB
ON B.ID = TB.ID
LEFT JOIN TeamJoins TJ
on TB.Team_ID = TJ.Team_ID
WHERE
TB.Team_ID IN NOT NULL
OR B.mem_id ... rest of query
What I ended up doing is;
I added an extra column to my blabbing table called team_id and set it to null as well as another field in my team_blabbing table called mem_id
Then I changed the insert script to also insert a value to the mem_id in team_blabbing.
After doing this I did a simple UNION ALL in the query:
SELECT
*
FROM
blabbing
WHERE
mem_id='$id' OR
fromid='$logOptions_id'
UNION ALL
SELECT
*
FROM
team_blabbing
WHERE
team_id
IN
($team_array)
ORDER BY
blab_date DESC
LIMIT 20
I am open to any thought on what I did. Try not to be too harsh though:) Thanks again for all the info.