SQL just do the opposite of this - mysql

SELECT Name FROM Anime LEFT JOIN anime_user2 ON anime_idAnime = idAnime WHERE users_userID = $userID
I want to get the exact opposite of this but it's not working with
WHERE users_userID != $userID
I get 4 Names, but I want every Name except those 4 Names.
Do you how to do this?

You can use NOT IN operator like this:
SELECT NAME
FROM ANIME a
WHERE a.NAME NOT IN (
SELECT Name
FROM Anime LEFT JOIN anime_user2 ON anime_idAnime = idAnime
WHERE users_userID = $userID
)
BEWARE: Depending on what is your table histogram, this query might be too slow to use it on a web page or application.

Related

Get results with multiple criterias

Lets say I have these tables
I want to get all matches for a concrete matchday along with bets for each match if its made by a concrete user. Basically avoid getting results from other users but still get all matches for the matchday. I tried WHERE match.matchday = 1 AND user.userId = 1 but this gives only the results where both the matchday and the userId match, so if there is no bet on a match from the user for the matchday it is not added to the results
The result should be like
Also I am open for suggestions if this is a good way to get what I want, or I should just use multiple requests to get the data and manage it in the application
There are multiple ways to achieve it with small tweaks.
Here are 2 working solutions.
SELECT *
FROM match
LEFT JOIN bet ON match.matchId = bet.matchId AND bet.userId = 1
LEFT JOIN user ON bet.userId = user.userId
WHERE match.matchday = 1
SELECT *
FROM match
LEFT JOIN bet ON match.matchId = bet.matchId
LEFT JOIN user ON bet.userId = user.userId
WHERE match.matchday = 1 AND (
bet.userId = 1 OR bet.userId IS NULL
)

How to get data from two tables from database with MySql

What I want to do is to get all of the information from one table where the username and the number is the same as in the table, and then on the same call I want to join two rows from a different table with the same title as the first table row named "title" (hopefully I got it clear enough).
Tried this:
`SELECT * FROM movies_in_theater
WHERE username = "${req.username}"
AND theater_number = '${req.theater}'
LEFT JOIN movie_info.key_exp, movie_info.key_exp_time
WHERE movies_in_theater.movie_title = movie_info.title
`
Like this:
SELECT t.username
, t.theater_number
, t.movie_title
, i.key_exp
, i.key_exp_time
FROM movies_in_theater t
LEFT
JOIN movie_info i
ON i.title = t.movie_title
WHERE t.username = ?
AND t.theater_number = ?

MySQL - Join two rows to two columns

I have two tables: user and photo. They look like this:
I need to perform a SELECT on the user table based on uuid, returning the url for both profile_photo and background_photo, if they exist.
These are essentially the final fields I need (the last two being JOINed from photo):
user.name, user.profile_photo_url, user.background_photo_url
WHERE user.uuid = SOME_UUID
Can somebody point me in the right direction with this statement?
SELECT user.name, photo_a.url AS profile_photo_url, photo_b.url as background_photo_url FROM user LEFT JOIN photo as photo_a ON user.profile_photo_uuid = photo_a.uuid LEFT JOIN photo as photo_b ON user.background_photo_uuid = photo_b.uuid WHERE user.uuid = SOME_ID
This should work for you
SELECT u.name,u.profile_photo_uuid,u.background_photo_url FROM user u, photo p
WHERE u.uuid = **userid** AND
( p.uuid = u.profile_photo_uuid OR p.uuid = u.background_photo_url);

Selecting columns using variables

Probably not the best description for a title but I really couldn't think of anything.
What I want to happen is that I can create a query that selects the type from listings using the userID as a relationship and then selecting the town.
TABLE listings
saleID userID type description
1 23 clothing nice clothing
2 45 clothing More nice Clothing
TABLE users
userID country county town
23 uk county townname1
24 uk county townname2
The variables are set in the url as a get eg) ?type=clothing&town=townname2
if (!isset($type)){
$query = "SELECT * FROM listings";
}
if (isset($type)) {
$query = "SELECT * FROM listings WHERE type = '{$type}'";
}
This is the bit i'm stuck on I want to get all listings with the variable $type 'clothing' and then select from users with the town of the variable $town
if (isset($town)) {
$query = "SELECT users.town listings.userID listings.type FROM listings
WHERE type = '{$type}'
INNER JOIN users
ON users.town = '{$town}'";
}
Hope I've explained this well enough to understand.
Please help me with this last query
SELECT u.town l.userID, l.type
FROM listings l
INNER JOIN users u on u.userID = l.userID
WHERE l.type = '{$type}'
AND u.town = '{$town}'
SELECT b.town, a.userID, a.type
FROM listings a
INNER JOIN users b
ON a.userID = b.userID
WHERE a.type = '{$type}' AND
b.town = '{$town}'
To further gain more knowledge about joins, visit the link below:
Visual Representation of SQL Joins
As a sidenote, the query is vulnerable with SQL Injection if the value(s) of the variables came from the outside. Please take a look at the article below to learn how to prevent from it. By using PreparedStatements you can get rid of using single quotes around values.
How to prevent SQL injection in PHP?

Unique records query

The following query is showing same user for multiple times, how to make it unique?
$query = 'SELECT a.connection_id, a.connect_from, a.connect_to,
b.userid, b.thumb, c.name, d.user_id, d.field_id,
d.value as bday, e.creator, e.id as videoprofile,
DATE_FORMAT(FROM_DAYS(DATEDIFF(NOW(),d.value)), "%Y")+0 AS age
FROM `#__community_connection` AS a
LEFT JOIN `#__community_users` AS b
ON a.connect_to = b.userid
LEFT JOIN `#__users` AS c ON a.connect_to = c.id
LEFT JOIN `#__community_fields_values` AS d
ON a.connect_to = d.user_id
LEFT JOIN `#__community_videos` AS e ON a.connect_to = e.creator
WHERE a.connect_from = "' . $uid .'" AND d.field_id = "3"
ORDER BY DAYOFMONTH( bday ) ASC';
Add DISTINCT after your SELECT to return only unique rows. So change the first line of your code to the following:
$query = 'SELECT DISTINCT a.connection_id, a.connect_from, a.connect_to, ...
[The rest of your query follows here.]
Not entirely sure what you're asking but I assume you want a unique identifacation method for your users?
Well in PHP you can hash their ip address and use that as their id, however it contains numbers and letters, so that'll make it unique i suppose :)
Here's how it'll look
.md5($data['post_ip']);
it'll appear different to everyone, for example, mine appears as the following
e36e263f082188a317f89e0dfef766ed
hopefully this is what you're looking for :)