Mysql - Verify if part of a result exists in another table - php

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

Related

How delete children and still display parents with PHP and SQL [duplicate]

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'

Get CSV from query which involves multiple tables

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.

Data not fetched from second table after using where clause PHP/MYSql (SQL Joins)

My sql query is to return title,date from one table and image name from another table using join based on Ad ID.
Here is the same.This works perfectly and fetches all data title, date from table 1 and image name from table 2.
SELECT wordpresswp_awpcp_ads.ad_title,
wordpresswp_awpcp_ads.ad_id,wordpresswp_awpcp_ads.ad_postdate,
wordpresswp_awpcp_media.name FROM wordpresswp_awpcp_ads
LEFT OUTER JOIN wordpresswp_awpcp_media on wordpresswp_awpcp_ads.ad_id = wordpresswp_awpcp_media.ad_id
Now for alphabet wise query i.e display ads staring with A,B.... I have passed a query in URL and rewrote the query as
SELECT wordpresswp_awpcp_ads.ad_title, wordpresswp_awpcp_ads.ad_id,
wordpresswp_awpcp_ads.ad_postdate,wordpresswp_awpcp_media.name FROM wordpresswp_awpcp_ads
LEFT OUTER JOIN wordpresswp_awpcp_media on wordpresswp_awpcp_ads.ad_id = wordpresswp_awpcp_media.ad_id
WHERE (wordpresswp_awpcp_ads.ad_title LIKE '$directory%' )
$directory takes alphabet passed from URL. With this query I am able to fetch data from table 1 i,e title name and date from not able to get the data from joined table i.e image name from table 2.
Any help would be appreciated.
Try this
SELECT wordpresswp_awpcp_ads.ad_title, wordpresswp_awpcp_ads.ad_id,
wordpresswp_awpcp_ads.ad_postdate,wordpresswp_awpcp_media.name
FROM wordpresswp_awpcp_ads
INNER JOIN wordpresswp_awpcp_media
ON wordpresswp_awpcp_ads.ad_id = wordpresswp_awpcp_media.ad_id
WHERE (wordpresswp_awpcp_ads.ad_title LIKE '$directory%' )
Your question is a bit unclear, but I guess you got into the classics.
When you add a where condition about your left joined table, then you get only rows which have that left join. So make sure you do not create a where-condition about wordpresswp_awpcp_media.
I think php variable is in single comm (') that's the problem please try
SELECT wordpresswp_awpcp_ads.ad_title, wordpresswp_awpcp_ads.ad_id,
wordpresswp_awpcp_ads.ad_postdate,wordpresswp_awpcp_media.name FROM wordpresswp_awpcp_ads
LEFT OUTER JOIN wordpresswp_awpcp_media on wordpresswp_awpcp_ads.ad_id = wordpresswp_awpcp_media.ad_id
WHERE (wordpresswp_awpcp_ads.ad_title LIKE "$directory%" )

MySql List all items from table 1 where item conditionally exists in table 2

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.

mySQL double filtering on the same query

I have a mySQL database which includes 3 tables to associate galleries with images. The images are stored in the table "files" and the galleries in the table "galleries", so I use a middle table called "gallery_files" for the association between them. The "gallery_files" table consists of the 2 IDs (F_ID, G_ID) of each of the main tables respectively.
The files/images can be associated with more than one gallery, so there can be records in the "gallery_files" of the same F_ID for different G_IDs.
Now, I would like to make a mySQL query which would bring all the images that ARE NOT associated with a given gallery already. So I use the following query:
(let's say for the gallery with ID: 9)
SELECT * FROM files
INNER JOIN gallery_files ON files.F_ID=gallery_files.F_ID
WHERE files.F_FILETYPE IN ('.jpg','.jpeg','gif','png')
AND files.F_DELETED = 0
AND gallery_files.G_ID <> 9
My problem is that if there is a record of an image associated with a different gallery it still brings it. I know why this happens, but I cannot think of a smart query to do the job. I know how to solve this by using 2 queries but I would like to avoid it, if possible.
Any thoughts?
Without a subquery, you can use an outer join, and check for NULL columns to only select those rows that don't match the inner join:
SELECT * FROM files
LEFT JOIN gallery_files ON files.F_ID=gallery_files.F_ID AND gallery_files.G_ID = 9
WHERE files.F_FILETYPE IN ('.jpg','.jpeg','gif','png')
AND files.F_DELETED = 0
AND gallery_files.G_ID IS NULL
By putting the G_ID condition in the ON clause, the JOIN joins each file to its gallery-9 row from the gallery_files table, if that exists, or to NULLs if it doesn't (since it's an outer join).
Then, by checking for NULL in the gallery_files columns (I checked G_ID, but any column would have worked), we only get those rows from files which were not in gallery 9.
I believe a NOT IN statement should do what you want.
Something along the lines of:
SELECT *
FROM files
WHERE files.F_ID NOT IN
(SELECT files.F_ID
FROM files
INNER JOIN gallery_files ON files.F_ID=gallery_files.F_ID
WHERE files.F_FILETYPE IN ('.jpg','.jpeg','gif','png')
AND files.F_DELETED = 0 AND gallery_files.G_ID = 9)

Categories