I need your help,
How do I Select from a parent table, only if child table (another table that depends on id of parent table) has rows in php?
For instance:
I have CATEGORY table and Items table. Where Category is the parent table and contains ct_id, ct_name, while Items is child table which contains it_id, ct_id (linked to parent table), it_name
PS: I don't need to select items, but I need only Categories ONLY if there are items linked to this table.
Thank you
You could use INNER JOIN which only returns records when there is a match on both tables.
SELECT DISTINCT a.* FROM Categories a INNER JOIN Items b on b.ct_id = a.ct_id
But it is more efficient to run a subquery:
SELECT *
FROM Categories
WHERE ct_id IN (SELECT ct_id FROM Items);
This is because in the first example it has to match the entire table first and then strip out all the duplicates using the DISTINCT keyword. The second example avoids the duplication by scanning the child table first.
This is known as a Semi Join. See here for more: https://dev.mysql.com/doc/refman/5.6/en/semi-joins.html
Related
I have a table cars(id, name) containing 20 rows. The other table carLog(username, car, count) contains rows which count the cars a player has bought (if there is no row if they haven't bought the car)
I want my query to return all twenty cars, and the extra join info, if they've got a row in the carLog table but I can't get it to work.
SELECT * FROM cars LEFT JOIN carLog ON cars.id=carLog.car
This is returning hundreds of rows, I want it to return 20 rows (one for each car), and the extra info in the row if the username has purchased the car:
WHERE carLog.username='Juddling'
I have no idea if I'm meant to be using GROUP BY, WHERE or another type of join!
Move the username condition from the WHERE clause to the ON clause.
SELECT *
FROM cars
LEFT JOIN carLog
ON cars.id=carLog.car
AND carLog.username='Juddling'
The WHERE clause is applied when the JOIN is already completed. This means, it will discard the NULL rows that the LEFT JOIN added.
As you are limiting the table from the outer join, you have to put the condition in the on, not the where:
select * from cars
left join carLog on cars.id = carLog.car and carlog.username = 'Juddling'
I have two tables in Mysql, one is all the data I need to display to users and in another simply a list of URLs of the items that were deleted.
What I want to do is select all the results as long as the element does not exist in the "deleted items" table.
In my table of "Deleted_Items" I have a list of URLs of the type
https://example.com/video/123456/
But in my table "Items" the URL column contains the following:
https://example.com/video/123456/dogs_and_cats/
I would need to do something like this (pseudocode):
SELECT id, url, thumb FROM Items
WHERE Items.url NOT CONTAINS Deleted_Items.url
P.S:
I had a similar case with two other tables but the difference was that in the "Items" table I had a list of IDs and in the "Deleted_Items" table I also had a list of IDs, so I applied the following query:
SELECT id, url, thumb
FROM Items
LEFT OUTER JOIN Deleted_Items
ON (Items.url = Deleted_Items.url)
WHERE Deleted_Items.url IS NULL
LIMIT 30
You could use a left join check for deleted_item.url is null
SELECT Items.id, Items.url, .Itemsthumb
FROM Items
LEFT JOIN deleted_Items ON Items.url = deleted_Items.url
where deleted_Items.url is null
looking to your added data sample could be you need a like comparision
SELECT Items.id, Items.url, .Itemsthumb
FROM Items
LEFT JOIN deleted_Items ON deleted_Items.url like concat(Items.url , '%')
where deleted_Items.url is null
I'm trying to make a query to extract elements from 2 tables, which are linked via another table.
So I have 3 tables:
authors
- id, name, book
category
- id, name, description
category-author
- id, idauthor, idcategory
Now I want to make a query to make the following output:
row: authors.id, authors.name, authors.book, category.name
I don't know what category's are linked using the 2 tables only, so I need to use the last one, the category-author table. The id's of both the author and the category are linked via that table.
I got the following query:
SELECT authors.id, authors.name, authors.book, category.name FROM category, author LEFT JOIN SELECT ??
I'm stuck at the remaining part of the query.
Also when I have this query, can I just extract a CSV with phpmyadmin?
You can get related information from different tables using table joins. Relations between tables should be specified using foreign keys (i.e. the column idcategory from category-author is presumably a foreign key that refers to primary key column category.id). In a join clause, you merely specify which tables are to be joined and on what column:
SELECT table1.col1, table2.col2
FROM table1
JOIN table2 ON table1.pkCol = table2.fkCol
This means you can't specify any SELECT or FROM clauses within a JOIN clause. The columns you wish to select from joined tables are all specified in the initial SELECT statement, and you only specify one table in the FROM clause, from which you subsequently perform the table joins.
In your case, I think this should get you started:
SELECT authors.id, authors.name, authors.book, category.name
FROM category
LEFT JOIN category-author ON category-author.idcategory = category.id
LEFT JOIN authors ON authors.id = category-author.idauthor
I'm not sure how familiar you are with foreign keys, primary keys and table joins, so I won't elaborate any more on this. I think specifying multiple tables in a FROM clause is bad practice, even if your database system still supports it (Related question).
From then on, you can easily export the results from within PhpMyAdmin, as there is an export button for every table overview, including query results.
Table-1 parts is a full parts catalog. Primary index field is PartID (int, auto-increment).
Table-2 inventory is a list of partIDs connected to various distributors. Important fields are DistID and PartID. Primary index field is InvID (int, auto-increment).
In Table-2, there will be a unique entry (InvID) for each part a distributor has, so many duplicate DistID/PartID entries.
Given a particular DistID, I must first get a list of all PartIDs associated with that DistID (inventory Table-2), and then SELECT * FROM parts (Table-1) for all those PartIDs.
The end result set is a list of all part information (T1) for each unique part held by a distributor (T2).
I'd like to do this using a single mysql query.
What you want is to start with a distinct list of the inventory for that distributor. This is our base query (replace the question mark with your actual distributor ID):
SELECT DISTINCT partID FROM inventory WHERE DistID = ?
Then we modify that query to join on the parts table to pull back the part information:
SELECT DISTINCT i.partID, p.* FROM inventory i INNER JOIN parts p ON i.partID = p.partID WHERE i.DistID = ?
thats not a PHP question at all.
Anyway, if I understood your question your should try this:
SELECT parts.* FROM parts WHERE parts.PartID IN (SELECT invetory.PartID FROM invetory WHERE invetory.DistID = 'wanted_DistID');
You should change 'wanted_DistID' for you wanted DistID.
Hope I could help.
I have a categories table set up as so [id, name, parent_id] and a items table [id, name, category_id, visible]. What I'm trying to do is create a query that will return all the ids of all non-empty categories, non empty being that it or one of it's children has at least one item belonging to it. What would be the best way to do this in MySQL?
edit
SELECT DISTINCT category_id FROM Items
This works for categories containing items, but I also need the parent categories of all item containing categories. This query will be used as a subquery along with some other filters.
Top Level Category
->Second Level Category
-->Third Level Category
--->Item 1
--->Item 2
Maybe off topic, but I think it is still worth referencing: Extensive Article on Managing Hierarchical Data in MySQL.
All non-empty categories, and only those, have items with category_id pointing at them, therefore you could just select category_ids from items table:
SELECT DISTINCT category_id FROM Items
As far as I know, you can't select all the ancestors of these categories in one query, however you might want to use another tree model.
With the nested set model, your query could look like this:
SELECT DISTINCT c.id FROM Categories c JOIN Items ON c.id = category_id JOIN Categories ancestors ON c.lft BETWEEN ancestors.lft AND ancestors.rgt
I'm not sure if it'll work, but you can try.