I have a MySQL query that is trying to get all the Pages that contain Data like 'word%'. I have a many-to-many table called Pages2Data. It seems that to do this, I need to have an inner join connecting the Pages to the Pages2Data table, and then another inner join connecting Pages2Data to Data.
The following did not work, because the nested SELECT clause can return more than one row. I'm not sure how to fix it though:
SELECT * FROM `Pages`
INNER JOIN `Pages2Data` ON
(`Pages2Data`.`DataID`=(SELECT `DataID` FROM `Data` WHERE `DataWord` LIKE 'word%'))
AND `Pages`.`PageID`=`Pages2Data`.`PageID`;
SELECT * FROM `Pages`
INNER JOIN `Pages2Data`
ON `Pages`.`PageID`=`Pages2Data`.`PageID`
INNER JOIN `Data` ON `Data`.`DataID`= `Pages2Data`.`DataID`
WHERE `DataWord` LIKE 'word%';
First the problems with your query:
the join condition should be next to the ON clause
there is no WHERE clause in the outer most query
Fixed query:
SELECT * FROM `Pages`
INNER JOIN `Pages2Data` ON `Pages`.`PageID`=`Pages2Data`.`PageID`
WHERE `Pages2Data`.`DataID`= (SELECT `DataID` FROM `Data` WHERE `DataWord` LIKE 'word%');
Alternative query:
SELECT `PG`.*
FROM `Pages` `PG`
INNER JOIN `Pages2Data` `PD` ON `PD`.`PageID` = `PG`.`PageID`
INNER JOIN `Data` `DA` ON `PD`.`DataID` = `DA`.`DataID`
WHERE `DA`.`DataWord` LIKE 'word%';
try this
SELECT * FROM `Pages` p, `Pages2Data` p2d, `Data` d
WHERE p.`PageID` = p2d.`PageID`
AND p2d.`DataID` = d.`DataID`
AND `DataWord` LIKE 'word%'
You can check 'Like' in where clause.
SELECT * FROM
`Pages`
INNER JOIN
`Pages2Data` ON (`Pages`.`PageID`=`Pages2Data`.`PageID`)
WHERE
`DataWord` LIKE 'word%'
SELECT *
FROM Pages
INNER JOIN Pages2Data
ON Pages.PageID = Pages2Data.PageID
INNER JOIN Data
ON Pages2Data.DataID = Data.DataID
WHERE DataWord LIKE 'word%'
Related
I am trying to create a view in MySQL, however, an error occured:
Duplicate column name 'profil_id'
This is my request:
CREATE VIEW my_view
AS SELECT * FROM `profils` AS `p`
INNER JOIN `table_A` ON table_A.profil_id = p.id
INNER JOIN `table_B` ON table_B.profil_id = p.id
WHERE p.id = '1';
I know this is because I have to create an alias for profil_id, but I don't know how to figure out that with an INNER JOIN...
INNER JOIN `table_A` ON table_A.profil_id = p.id AS table_A_profil_id
Does not work.
Thank you for your help.
You can use the following:
CREATE VIEW my_view AS
SELECT `p`.*, `table_A`.`profil_id` AS table_A_profil_id, `table_B`.`profil_id` AS table_B_profil_id
FROM `profils` AS `p`
INNER JOIN `table_A` ON table_A.profil_id = p.id
INNER JOIN `table_B` ON table_B.profil_id = p.id
WHERE p.id = '1';
You can't use * in this case because you INNER JOIN two tables with the same column name. You need to list the columns for these two tables and set an alias to the duplicate column names.
I just used this code, and working properly with me:
SELECT
* FROM `order` as o
LEFT JOIN `services` as s ON s.`id` = o.`service_id`
LEFT JOIN `users` as u ON u.`id` = o.`users_id`
LEFT JOIN `files` as f ON f.`order_id` = o.`id`
but when I try to choose some fields from the 1st table, the results not showing the other tables
SELECT
o.`id` AS `id`,
o.`service_id`,
o.`extras`,
o.`quantity`,
o.`price`,
o.`links`,
o.`keywords`,
o.`status_id`,
o.`users_id`,
o.`date`,
o.`notes`,
o.`c_reason`,
o.`agent_star`
FROM `order` as o
LEFT JOIN `services` as s ON s.`id` = o.`service_id`
LEFT JOIN `users` as u ON u.`id` = o.`users_id`
LEFT JOIN `files` as f ON f.`order_id` = o.`id`
I don't know what is the exact error on the 2nd code, I need to show all columns from the tables: services, users & files
all columns or just defined columns
You when you select * from a join you are selecting all results from all tables involved.
When you are specifying orders you are only getting the results as they pertain to the orders table you get the same thing if you were to do SELECT o.* so if you want to see shared fields from different tables you have to specify them in your select statement as well.
Basically you're seeing the Different between SELECT * and SELECT o.*
This code is working for me, thanks everybody :)
SELECT
o.`id` AS `id`,
o.`service_id`,
o.`extras`,
o.`quantity`,
o.`price`,
o.`links`,
o.`keywords`,
o.`status_id`,
o.`users_id`,
o.`date`,
o.`notes`,
o.`c_reason`,
o.`agent_star`
s.*
u.*
f.*
FROM `order` as o
LEFT JOIN `services` as s ON s.`id` = o.`service_id`
LEFT JOIN `users` as u ON u.`id` = o.`users_id`
LEFT JOIN `files` as f ON f.`order_id` = o.`id`
I am using SQL for the first time and can't seem to figure out how can I convert this query into a join. I want to do so, because I read this:
Join vs. sub-query
SELECT `bookings`.* FROM `bookings` WHERE `bookings`.`user_id` IN
(SELECT `users`.`id` FROM `users` WHERE `users`.`phone` = 9999999999)
I want to find only those bookings whose users belong in the user table with a given phone number.
I tried using a join, but I don't understand what the possible condition of join should be.
Thanks a lot!
You need something like that:
SELECT b.* FROM `bookings` b
INNER JOIN `users` u
ON b.user_id = u.id
WHERE u.phone = 8860990440
SELECT `bookings`.* FROM `bookings` INNER JOIN `users` ON `users`.`id` = `bookings`.`user_id` WHERE `users`.`phone` = 9999999999
I have three queries gives me result individually correct but my requirement is i need all result in single query only so how should i proceed?
select * from user_post_like
inner join user_post on user_post_like.postID = user_post.postID
inner join Users on Users.userID=user_post_like.userID
where (user_post.poster='$uid' AND user_post_like.userID!='$uid')
ORDER BY likeID DESC;
select * from user_post_comment
inner join user_post on user_post_comment.postID = user_post.postID
inner join Users on Users.userID=user_post_comment.commenter
where (user_post.poster='$uid' AND user_post_comment.commenter!='$uid')
ORDER BY commentID DESC;
select * from user_post_share
inner join user_post on user_post_share.postID = user_post.postID
inner join Users on Users.userID=user_post_share.Share_user_id
where (user_post.poster='$uid' AND user_post_share.Share_user_id!='$uid')
ORDER BY shareID DESC;
Since you're joining the tables anyway, you can put columns from all in your select - and keep your statement readable. If you have duplicate column names (from different tables) you may need to aggregate them with functions and group by.
SELECT s.*, p.*, u.*
FROM user_post_share s
INNER JOIN user_post p ON s.postID = p.postID
INNER JOIN Users u ON u.userID = p.poster
WHERE (p.poster='$uid' AND s.Share_user_id != '$uid')
ORDER BY shareID DESC
try sumthing like this
select * from user_post_like,user_post_comment,user_post_share <inner joins> <where conditions>
Currently I have the following command:
SELECT * FROM `clients`
WHERE `ID`=(SELECT `clientID`
FROM `websites`
WHERE `name` LIKE "%my name%")
LIMIT 0,20
I wish to use an inner join, because AFAIK that is how it should be handled.
I have no idea how to do it... Here is all I could think of:
SELECT *
FROM `clients`
WHERE INNER JOIN `websites` ON `websites`.`name` LIKE "%lead%"
LIMIT 0,20
SELECT c.* FROM `clients` c
INNER JOIN `websites` w ON w.clientID = c.ID
WHERE w.`name` LIKE '%lead%'
LIMIT 0,20