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.
Related
I need to write a query to join 3 tables.
My tables are:
ucommerce_customer
ucommerce_order
ucommerce_order_line
All 3 tables have a column called order_id.
The table ucommerce_order has a column called order_status.
When the order_status is set to "open" I want to display the order details.
ResultSet myRs = myStmt.executeQuery
("SELECT * FROM ucommerce_customer
INNER JOIN ucommerce_order
INNER JOIN ucommerce_order_line
WHERE ucommerce_order.order_status = 'open'");
My query ignores the order status and displays all orders, open and closed.
Also I have several products so ucommerce_order_line has several entries for the same order_id, my query displays duplicate entries and it duplicates the entire list as well.
How can I write a query that will show only open orders without duplicating everything?
In MySQL, the on/using clause is optional. This is very sad because someone can make mistakes like you did. Your question only mentions one column, so perhaps that is all that is needed for the join:
SELECT *
FROM ucommerce_customer INNER JOIN
ucommerce_order
USING (orderId) INNER JOIN
ucommerce_order_line
USING (OrderId)
WHERE ucommerce_order.order_status = 'open';
I would be surprised if the customer table really had a column called OrderId (seems like a bad idea in most situations), so the first USING clause might want to use CustomerId.
I would recommend to use a natural join instead. Maybe that's where the errors are coming from.
The duplicates can be removed by running SELECT DISTINCT * ...
I have one table service_contacts which can contain listids from the lists table and contactids from the contacts table. contact_list_relationship has the relationships between contacts and lists tables.
I'm trying to pull all the contacts which could be in a contactid in service_contacts, or in a listid (which each list contains contactids).
SELECT d.* FROM service_contacts a
LEFT JOIN lists b
ON a.calllistid=b.listid
LEFT JOIN contact_list_relationship c
ON c.listid=b.listid
INNER JOIN contacts d
ON d.contactid=c.contactid OR d.contactid=a.contactid
WHERE a.memberid=12345
This runs, and pulls the expected results. So far ... I'm just wondering if there might be a better way.
Assuming that the fields you are joining on are indexed / or preferably defined with FOREIGN KEY constraints (which enforce indexing) you should be fine.
However MySQL does not always use indexes even if they are available. To check the indexes are being used you can run an explain on your statement i.e.
EXPLAIN
SELECT d.* FROM service_contacts a
LEFT JOIN lists b
ON a.calllistid=b.listid
LEFT JOIN contact_list_relationship c
ON c.listid=b.listid
INNER JOIN contacts d
ON d.contactid=c.contactid OR d.contactid=a.contactid
WHERE a.memberid=12345;
the results will inform you which indexes are being used in the statement. If the results are not as expected you can force Mysql to use the indexes withby stating 'force key for join indexname after each reference to a table.
I have two tables as follows..
campaigns(campaignID, title)
campaignMailList(campaignID, Sent)
(The 2 tables are bigger.. but for this purpose I only need these columns)
I want to do generate a query that will DELETE all rows in campaignMailList where it cannot find a join in the campaigns table on the campaignID.
Can anyone suggest how I do this?
Translating word by word your own question into SQL:
delete from campaingMailList where
campaingId not in (select campaingid from campaigns)
You could try the NOT IN clause.
Like
DELETE FROM `campaignMailList` as cml
WHERE cml.campaignid NOT IN ( SELECT campaignID FROM campaigns )
Not sure if it is the fatest way to do this
Try this query -
DELETE cml FROM campaignMailList cml
LEFT JOIN campaigns c
ON c.campaignID = cml.campaignID
WHERE c.campaignID IS NULL
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')
I tried looking for a question such as this on SO but a lot of them were outer joins with more complicated select clauses whereas my question is more simple and should be a faster reference for newbies at MySQL.
Being used to Oracle SQL, I was trying to join three tables and perform a delete like so:
DELETE * FROM tbl_login, tbl_profile, images
WHERE tbl_login.loginid = tbl_profile.loginid
AND tbl_profile.loginid = images.loginid
AND loginid = 'derek';
In MySQL my attempt is:
DELETE FROM tbl_profile, images, tbl_login
USING tbl_profile INNER JOIN images INNER JOIN tbl_login
WHERE tbl_profile.loginid = images.loginid
AND images.loginid = tbl_login.loginid
AND loginid='derek';
I ran this in the SQL section of PHPMyadmin and it told me that loginid was ambiguous which I thought was funny because if I'm joining the three tables why would it be ambiguous? So I edited it and made it
tbl_login.loginid = 'derek'
that deleted the correct row from the tbl_login table but it ended up deleting all the rows from my other tables. What am I doing wrong here?
Remove tbl_profile and images from your FROM clause.
I think your query should look something like this (note the different way the join conditions are defined):
DELETE FROM tbl_profile, images, tbl_login
USING tbl_profile INNER JOIN images ON images.loginid = tbl_profile.loginid
INNER JOIN tbl_login ON tbl_login.loginid = tbl_profile.loginid
WHERE tbl_login.loginid='derek';
I assume you want to delete the rows from all 3 tables. If you only want to delete from tbl_login, the previous answer tells you how to do it :)