I have been trying to get this working for quite a while now but it just doesn't seem to work, maybe it is is not even possible, what i am wanting to do is to perform a MySQL join query using like, such as this example i found...
SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.col LIKE '%' + Table2.col + '%'
but it just doesn't seem to work at all, any help that can be given would be brilliant, thanks !
Try
SELECT *
FROM Table1
INNER JOIN Table2 ON Table1.col LIKE CONCAT('%', Table2.col, '%')
MySQL does string concatenation differently from other databases, so in case you want to port your app, you need to have an alternate version where || is used as concatenation operator, as mentioned by Michael in another answer. This operator doesn't work in MySQL though, since it means or.
How about this instead:
SELECT * FROM Table1, Table2 WHERE Table1.col LIKE '%'+Table2.col+'%';
Since you're selecting everything from both tables anyway, this would probably get you there.
SELECT p.products_id, pd.products_name, p.products_model
FROM products_description pd
JOIN products p ON p.products_id = pd.products_id
WHERE pd.products_name LIKE CONCAT( '%', p.products_model, '%' )
LIMIT 0 , 100
First off you have to restrict your request by (p.products_id = pd.products_id) and LIMIT. And look what time it took. After that you can go and make comparison with (WHERE). If you gonna compare directly within JOIN you will put down your db if there are at list 20 thousands items. Beware.)
Related
I'm trying to run this code but i'm getting errors.
Any ideas why?
SELECT notes.project_id, notes.title, notes.description
FROM notes, project_members
WHERE notes.title LIKE '%second%' OR notes.description LIKE '%second%'
WHERE notes.project_id = project_members.project_id AND project_members.user_id = '7'
Since you only can have one WHERE clause you could use a inner join like this:
SELECT notes.project_id, notes.title, notes.description
FROM project_members pm
INNER JOIN notes ON notes.project_id = pm.project_id AND pm.user_id = 7
WHERE notes.title LIKE '%second%' OR notes.description LIKE '%second%'
You have two WHERE clauses, but single SELECT only allows one. But, this would easily be fixed if you used proper, explicit JOIN syntax:
SELECT n.project_id, n.title, n.description
FROM notes n JOIN
project_members p
ON n.project_id = p.project_id
WHERE (n.title LIKE '%second%' OR n.description LIKE '%second%') AND
p.user_id = '7';
Note:
Always use proper, explicit JOIN syntax. Never use commas in the FROM clause.
Table aliases make the query easier to write and to read.
I'm pretty sure your WHERE conditions are missing parentheses, for your intended logic.
If user_id is a number of any sort, then don't use single quotes around "7". This can confuse both the compiler and people.
There should be single WHERE clause.
SELECT notes.project_id, notes.title, notes.description
FROM notes, project_members
WHERE
(notes.title LIKE '%second%' OR notes.description LIKE '%second%')
AND notes.project_id = project_members.project_id AND project_members.user_id = '7';
Query would look something above to AND between your LIKE conditions.
You may also consider using JOIN, Like this.
SELECT notes.project_id, notes.title, notes.description
FROM notes
JOIN project_members
ON notes.project_id=project_members.project_id
WHERE
(notes.title LIKE '%second%' OR notes.description LIKE '%second%')
AND project_members.user_id = '7';
i am having a little trouble with this part in my lecture over SQL-Basics.
I think it is not very well explained and i am having a hard time finding any good information on the internet. In my book it says: "It is almost allways an error if two tuple attributes are not linked by an explicit join predicate: "
And then this example:
SELECT s.Name, c.Name AS Contact, c.Phone
FROM Suppliers AS s, ContactPersons AS c
WHERE s.Name = 'Shop Rite'
AND c.Phone LIKE '+49 351%'
No where is explained what an explicit join predicate is. For me this example just looks fine.
Beforehand there was a similair example:
SELECT s.Name, c.Name AS Contact, c.Phone
FROM Suppliers AS s, ContactPersons AS c
WHERE s.SuppID = c.SuppID
Which is an ok Join as the books says. I dont really understand what the difference is and what exactly is a JOIN-Predicate?
Also i am sorry for any grammer-mistakes (I am not a native speaker)
Thx in advance!
Implicit join syntax:
SELECT *
FROM Table1 , Table2
WHERE Table1.id = Table2.id
Explicit join syntax:
SELECT *
FROM Table1
JOIN Table2
ON Table1.id = Table2.id
Implicit syntax is fine and it will work, but it's not suggested. It is confusing syntax and may lead to many mistakes , mostly when dealing with more then two tables and when you need to LEFT JOIN (the stupid plus sign) . You should make it as a habit to use only the proper syntax of a join.
Here is an example for a query with 6 tables combining LEFT JOINs :
SELECT <columns>
FROM YourTable,AnotherTable,ThirdTable,FourthTable,AnotherTable2,AnotherTable3
WHERE YourTable.id = AnotherTable.id(+) AND
YourTable.sec_id = AnotherTable.Sec_Id(+) AND
AnotherTable.id (+) = ThirdTable.id(+) AND
YourTable.id = FourthTable.id AND
FourthTable.Date = ...
.......
As you can see, I didn't put even half of the conditions, assuming there could be a lot more conditions and it looks like crap.
HI I have the following query:
SELECT PAS_User.user_user_id,
PAS_User.user_city,
PAS_User.user_company,
PAS_User.user_country,
PAS_User.user_account_type,
PAS_User.user_account_premium,
PAS_User.user_sign_up_date,
PAS_User.user_first_name,
PAS_User.user_last_name,
PAS_User.user_avatar_url,
PAS_User.user_cover_image_url,
PAS_User.user_bio,
PAS_User.user_sector,
PAS_User.user_job_type,
(SELECT COUNT(*) FROM `PAS_Follow` WHERE `folw_follower_user_id`=:sid) AS user_following_count,
(SELECT COUNT(*) FROM `PAS_Follow` WHERE `folw_followed_user_id`=:sid) AS user_followed_count,
(SELECT COUNT(*) FROM `PAS_Post` WHERE `post_user_id`=:sid) AS user_post_count,
(SELECT COUNT(*) FROM `PAS_Follow` WHERE `folw_follower_user_id`=:sid AND `folw_followed_user_id`=:cid) AS user_this_user_is_following,
(SELECT COUNT(*) FROM `PAS_Follow` WHERE `folw_followed_user_id`=:cid AND `folw_follower_user_id`=:sid) AS user_this_user_is_followed
FROM PAS_User
WHERE `PAS_User`.`user_user_id`=:sid
Which is designed to get counts from other tables for a profile page and the basic user details where :sid = 1 and :cid = 2.
The question is, is there any better way of achieving this in perhaps a smaller query or in a cleaner way?
The tables used are
PAS_User , PAS_Follow & PAS_Post
Thanks
Justin
You should denormalize all those counter fields and only update them when a user posts something, or presses a Follow button. Your current query is going to blow up your database server in the foreseeable future if your website gets actual active users.
I'm going to be honest. I don't really like nested select statements (select within select). However, in your case, the alternative may be uglier and prone to performance errors. The temptation is to do something like:
select . . .
from PAS_User u left outer join
(select folw_foller_user_id, count(*) as user_following_count
from PAS_Follow pf
group by folw_foller_user_id
) pf
on pf.folw_foller_user_id = u.user_user_id left outer join
. . .
Fine. But this will perform much worse than your original query, because of the outside filter on user_user_id. To fix this, you would repeat the condition in the subquery:
select . . .
from PAS_User u left outer join
(select folw_foller_user_id, count(*) as user_following_count
from PAS_Follow pf
where folw_foller_user_id = :sid
group by folw_foller_user_id
) pf
on pf.folw_foller_user_id = u.user_user_id left outer join
. . .
Or even:
select . . .
from PAS_User u cross join
(select count(*) as user_following_count
from PAS_Follow pf
where folw_foller_user_id = :sid
) pf
on pf.folw_foller_user_id = u.user_user_id left outer join
. . .
And I might even argue that repeating the condition in one subquery would be good. I cannot make that argument for five subqueries.
To make this clear, the best syntax is to user window functions:
select . . .
count(*) over (partition by folw_fllower_user_id) as user_following_count,
. . .
Alas, MySQL does not support window functions. A very reasonable replacement, in my opinion, are subqueries (which would normally be correlated). This supports your initial syntax.
I am currently running into an issue where, when I use a "LIKE" in my query I get the result in 2 seconds. But when I use the '=' instead, it takes around 1 minute for the result to show up.
The following is my query:
QUERY1
The following query takes 2 seconds:
`select distinct p.Name from Timeset s
join table1 f on (f.id = s.id)
join table2 p on (p.source=f.table_name)
join table3 d on (d.Name = p.Name) WHERE
s.Active = 'Y' AND **p.sourcefrom like '%sometable%'`
QUERY2
The same query replacing the 'like' by '=' takes 1 minute:
select distinct p.Name from Timeset s
join table1 f on (f.id = s.id)
join table2 p on (p.source=f.table_name)
join table3 d on (d.Name = p.Name) WHERE
s.Active = 'Y' AND **p.sourcefrom = 'sometable'
I am really puzzled because I know that 'LIKE' is usually slower (than '=') since mysql need to look for different possibilities. But I am sure why in my case, "=" is slower with such a substantial difference.
thank you kindly for the help in advance,
regards,
When you use = MySQL is probably using a different index compared to when you use LIKE. Check the output from the two execution plans and see what the differnce is. Then you can FORCE the use of the better performing index. Might be worth running ANALYZE TABLE for each of the tables involved.
I am having following database schema, I want to fetch name of all categories with no of quotes related to that category . The query that i wrote giving me one row only can u please tell me the resource efficient query.
SELECT SC.Name, Count(*) AS Quotes
FROM status_categories AS SC
INNER JOIN status_quotes AS SQ ON SC._id = SQ._category_id
GROUP BY SC.Name
SELECT status_categories.NAME, COUNT(status_quotes.category_id)
FROM status_categories JOIN status_quotes ON status_categories._id = status_quotes.category_id
GROUP BY status_categories._id;
Try the following:
SELECT `c`.`name`, COUNT(*) AS `Number of quotes`
FROM `status_categories` AS `c`
INNER JOIN `status_quotes` AS `q`
ON `q`.`category_id` = `c`.`_id`
GROUP BY `c`.`_id`;
EDIT
Feel free to leave out the ` character. But that is the safe way of doing it, even though it looks a bit nasty.