Add columns when using IN in mysql for the result - mysql

Following is my table for artists -
id name sex
1 harsh male
2 geet female
Following is my table for events -
id artist_id created_by
2 2 16
2 2 17
Following is my query -
SELECT * FROM `events` WHERE artist_id IN (SELECT id FROM `artists` WHERE name LIKE '%$search_term%')
But apart from events data I need to get artist name in the result as well, please let me know what I need to change in my query as I tried *, artists.name it wont worked.

Try this query
SELECT e.*
FROM `artists` AS a
JOIN `events` AS e ON e.artist_id = a.id
WHERE a.name LIKE '%$search_term%';

You need to select from two tables simultaneously. Use the join for that
SELECT artists.name, events.*
FROM artists
INNER JOIN events
ON artist.id = artist_id
WHERE
name LIKE '%search_term%'

Use a join instead of an IN
SELECT
e.*,
artists.name
FROM
`events` e
inner join `artists` a on e.artist_id = a.id
WHERE
name LIKE '%$search_term%'

Related

How to select from table where value from another table is greather than some value?

How could I select * from table movies where seeds > 10 in table torrents? Table movies has unique id, while table torrents has id that matches the movie id. There are few torrents for each movie and having just one that has seeds greather than 10 should be selected.
The only sql I have is this where it deletes all movies which don't have any torrents.
DELETE FROM movies WHERE NOT EXISTS ( SELECT id FROM torrents WHERE id=movies.id)
I'm not good at mysql at that level to make it, even with this example, which I also got from around here.
Help would be very much appreciated.
something like the following:
select *
from movies m
where exists (
select * from torrents t
where t.id = m.id and t.seeds > 10
);
id from torrents where seeds > 10
select * from movies where movie_id in (select id from torrents where seeds > 10);
you can also write it as
select a.id from movies a join torrents b on a.movie_id = b.id
where b.seeds > 10;
you need to join the second table
SELECT * FROM movies as m INNER JOIN torrents as t ON m.id = t.movie
WHERE t.seeds > 10

How to write SELECT query from 2 different tables based on result from 3rd table, while keeping order?

I have 3 tables where one table has 3 columns with foreign keys to the other two tables.
table album_posters_albums-
+---------+---------+---------+
| album_id|poster_id|albums_id|
+---------+---------+---------+
| 49 | 167 | NULL |
| 49 | NULL | 45 |
+---------+---------+---------+
album_id and albums_id references the album table and poster_id represents the poster table.
I need to
SELECT * FROM poster
WHERE poster_id IN (
SELECT poster_id
FROM album_poster_albums
WHERE album_id=49);
IF the poster_id IS NULL:
SELECT * FROM album
WHERE album_id IN (
SELECT poster_id
FROM album_poster_albums
WHERE album_id=49).
The problem is I need to keep the posters and albums in the same order as they occur in the album_posters_albums table.
I was sending a query to get the list of ids, then looping through each result and querying the db to get either the poster or album but that is obviously very inefficient when I should be able to do it in one query.
It sounds like you want to use INNER JOINS
SELECT album.*, poster.*
FROM album_poster_albums
INNER JOIN album ON album_poster_albums.albums_id = album.album_id
INNER JOIN poster ON album_poster_albums.poster_id = poster.poster_id
WHERE album_poster_albums.album_id = 49
Based on your comment about one row with a poster and one row with an album, UNION ALL might be what you're looking for. (We'd need to see more details about the tables and a few more rows to understand the ordering part.) This should give you an album row then a poster row for each album id.
Caveats: The number and the orders of columns in the album and poster tables must be the same. Also, the data types of those columns must be the same or compatible. (I haven't used a UNION, or UNION ALL, in a very long time.)
SELECT * FROM (
SELECT album.*
FROM album_poster_albums
INNER JOIN album ON album_poster_albums.albums_id = album.album_id
WHERE album_poster_albums.album_id = 49
UNION ALL
SELECT poster.*
FROM album_poster_albums
INNER JOIN poster ON album_poster_albums.poster_id = poster.poster_id
WHERE album_poster_albums.album_id = 49
)
ORDER BY album_id
DECLARE #rowId INT(11);
SET #rowId :=0;
SELECT * FROM(SELECT #rowId:=#rowId+1,t.album_id,album.*
FROM album_poster_albums t
INNER JOIN album ON album.albums_id = t.albums_id
WHERE t.album_id = 49
UNION
SELECT #rowId:=#rowId + 2,t.album_id,poster.*
FROM album_poster_albums s
INNER JOIN poster ON poster.poster_id = t.poster_id
WHERE t.album_id = 49) T
ORDER BY #rowId,t.album_id
I decided to create a new table with an auto increment field based on #beltouche comment. My Mysql is pretty rusty and I thought there may be a way using case or if null. I didn't need the unique id previously with how I wrote the queries.
In hindsight the solution is obvious.
SELECT * FROM (SELECT albums.*, 1 AS type, t.id
FROM album_poster_album t
INNER JOIN albums ON albums.album_id = t.albums_id
WHERE t.album_id = 49
UNION ALL
SELECT poster.*, 2 AS type, s.id
FROM album_poster_album s
INNER JOIN poster ON poster.posterID = s.poster_id
WHERE s.album_id = 49) T
ORDER BY t.id

How to get only matching records using LIKE Statement

How to get the matching records from both the tables
Matching Table
CREATE TABLE matching (
ID INT,
Name varchar(20)
);
INSERT INTO matching(ID,name) VALUES (1,'Child'),(2,'GrandChild'),(3,'parent')
TreeMatching
CREATE TABLE Treematching (
ID INT,
Name varchar(20)
);
INSERT INTO Treematching(ID,name) VALUES
(1,'Child-Foster'),
(2,'Child-Filly'),
(3,'Child-Ricky'),
(4,'GRandchild-Filmy'),
(5,'GRandchild-Freaky'),
(6,'GRandchild-Frim'),
(7,'Frim'),
(8,'None'),
(9,'parent-John')
How to get the matching records from Tree matching tables
output :
ID Name TName
1 Child Child-Foster
2 Child Child-Filly
3 Child Child-Ricky
4 GRandchild GRandchild-Filmy
5 GRandchild GRandchild-Freaky
6 GRandchild GRandchild-Frim
9 parent parent-John
How to get the same records using like statements I have tried using CONCAT .
You could try something like this, if you use SQL Server:
SELECT
t.ID,
m.Name,
t.Name
FROM matching AS m
INNER JOIN Treematching AS t
ON t.Name LIKE '%'+m.Name+'%'
While you try this if you use MySQL Server:
SELECT
t.ID,
m.Name,
t.Name
FROM matching AS m
INNER JOIN Treematching AS t
ON t.Name LIKE CONCAT('%',m.Name,'%')
Regarding the MySQL version, please have a look at the following link.
sqlfiddle
You might be able to do something like this (not tested, I don't have a sql-server instance):
SELECT M.Id, M.Name, T.Name
FROM matching AS M
INNER JOIN Treematching AS T
ON M.Name LIKE T.Name + '%';
Try the below query,
SELECT TM.ID,M.Name,TM.Name
FROM #matching AS M
INNER JOIN #Treematching AS TM ON TM.Name LIKE M.Name+'%'
SELECT A.ID,
B.NAME,
A.NAME AS TNAME
FROM TREEMATCHING A
INNER JOIN MATCHING B ON A.NAME LIKE B.NAME+'%'
ID NAME TNAME
----------- -------------------- --------------------
1 Child Child-Foster
2 Child Child-Filly
3 Child Child-Ricky
4 GrandChild GRandchild-Filmy
5 GrandChild GRandchild-Freaky
6 GrandChild GRandchild-Frim
9 parent parent-John

Mysql - Join query

I have the following 2 queries:-
Query 1:-
mysql> select mccid_c, id_c from contacts_cstm where accountnumber_c = '1601480000552527';
250762 | 475093000013882513
Query 2:-
mysql> select first_name, last_name from contacts where id = '475093000013882513';
John | Doe
id in contacts = id_c in contacts_cstm
I required a join query to get mccid_c, first_name, last_name in one query
Thanks!
This is a classic usecase of the join syntax:
SELECT mccid_c, first_name, last_name
FROM contacts_cstm cc
JOIN contacts c ON c.id = cc.id_c
WHERE c.id = '475093000013882513'
You can use multiple tables in your single SQL query. The act of joining in MySQL refers to smashing two or more tables into a single table.
You can use JOINS in SELECT, UPDATE and DELETE statements to join MySQL tables.But firstly you need a common column (attribut),suppose it is 'tutorial_author' between the two tables that you want to join it.
SELECT a.mccid_c, b.first_name , b.last_name
FROM contacts_cstm a, contacts b
WHERE a.tutorial_author = b.tutorial_author and a.accountnumber_c = '1601480000552527' and b.id = '475093000013882513';
I hope it will help you, best regards
Try this.
Select a.mccid_c, b.first_name, b.last_name from contacts_cstm a inner join contacts b where a.id_c=b.id;

MySql select Not Work

I have two table .
one meal table
id type
1 lunch
2 lunch
3 dinner
two user_history table
ID Meal_id User_id create
1 2 4 1404638939
Now I want to select all meal from table one but have condition
if table two meal_id match with Meal table id and that create date same as current date then skip that row from table one
I use this code but not work correctly
SELECT m.* FROM `meal` AS m LEFT JOIN user_history
ON user_history.meal_id != m.id and date(FROM_UNIXTIME(user_history.create))!=CURRENT_DATE() where m.meal_type = 'Lunch'
SELECT m.* FROM `meal` AS m
LEFT JOIN user_history
ON user_history.Meal_id != m.id and date(FROM_UNIXTIME(user_history.create))=CURRENT_DATE()
where m.type LIKE "%lunch%"
If I correctly understood the question, I'd say:
SELECT m.* FROM `meal` AS m
WHERE m.meal_type = 'Lunch'
AND m.id NOT IN (
SELECT meal_id FROM user_history
WHERE date(FROM_UNIXTIME(user_history.create))=CURRENT_DATE()
)