MySQL SELECT INNER JOIN and LIKE - mysql

I whant to do something like this:
SELECT
s.name,
COUNT(r.request_log_id) AS NumberOfRequests
FROM
station AS s
LEFT JOIN request_log AS r ON r.requested LIKE '%s.station_id%'
GROUP BY 1
But this gives 0 in the NumberOfRequests and i think it is becouse its hardcoded s.station_id its running LIKE on.
Does anyone know how i can do this?
For your information, r.requested is a string whit some data in it and one example is
/StationInfo - stationID = "262" - Status = 0
So it is the staitonid i whant to get LIKE on and cound how many rows in request_log table it is in that have the station id for the first table named station

First approach:
SELECT
s.name,
COUNT(r.request_log_id) AS NumberOfRequests
FROM
station AS s
LEFT JOIN request_log AS r
ON r.requested LIKE concat( '%',s.station_id, '%')
GROUP BY 1
be careful with this non equijoin.
see you.

Related

SELECT query with JOIN returns duplicate of each row

I am running a SELECT query to return addresses in a table associated with a certain "applicant code" and I'd like to join a table to also return (in the same row) the name of that applicant.
Therefore my query as of now is
SELECT a.id, a.created_at, a.updated_at, a.code, a.applicant_code, a.form_code, a.address_line_1, a.address_line_2, a.town_city, a.county_state, a.country, a.post_code, a.start_date, a.end_date, a.type, ap.first_name, ap.last_name
FROM sfs_addresses a
JOIN sfs_personal_details ap ON a.form_code = ap.form_code
WHERE a.form_code = ? AND a.applicant_code = ?
The query works, and I get the right columns and values in each row, but it returns 2 of each so like
ID
===
1
1
2
2
3
3
4
4
If I remove the JOIN it works fine. I have tried adding DISTINCT (makes no difference) I'm lost.
EDIT: Based on this answer and the comments, the OP realized that the JOIN condition should be on applicant_code rather than form_code.
You have duplicates in the second table based on the JOIN key you are using (I question if the JOIN is correct).
If you just want one row arbitrarily, you can use row_number():
SELECT a.*, ap.first_name, ap.last_name
FROM sfs_addresses a JOIN
(SELECT ap.*,
ROW_NUMBER() OVER (PARTITION BY ap.form_code ORDER BY ap.form_code) as seqnum
FROM sfs_personal_details ap
) ap
ON a.form_code = ap.form_code
WHERE a.form_code = ? AND a.applicant_code = ?;
You can replace the columns in the ORDER BY with which result you want -- for instance the oldest or most recent.
Note: form_code seems like an odd JOIN column for a table called "personal details". So, you might just need to fix the JOIN condition.
relation between 2 tables one to many to return non duplicate use distinct
SELECT distinct a.id, a.created_at, a.updated_at, a.code, a.applicant_code, a.form_code, a.address_line_1, a.address_line_2, a.town_city, a.county_state, a.country, a.post_code, a.start_date, a.end_date, a.type, ap.first_name, ap.last_name
FROM sfs_addresses a
JOIN sfs_personal_details ap ON a.form_code = ap.form_code
WHERE a.form_code = ? AND a.applicant_code = ?

MYSQL get spacific table fields counts

I have the following 2 tables.
First is: idea_box
Second is: idea_box_voting
Now I want result something like this, based on 0 and 1 countfrom thumbs field.
I have never created sql query like this one so I am hoping that someone help me.
Thanks.
Try this:
SELECT a.idea_id, a.property_id, a.the_idea, a.user_id, a.added_date, a.status,
SUM(b.thumbs = 1) AS up, SUM(b.thumbs = 0) AS down
FROM idea_box a
LEFT JOIN idea_box_voting b ON a.idea_id = b.idea_id
GROUP BY a.idea_id;
Try this:
SELECT ib.*,
SUM(ibv.thumbs=1) as UP,
SUM(ibv.thumbs=0) as DOWN
FROM idea_box ib LEFT JOIN
idea_box_voting ibv on ib.idea_id=ibv.idea_id
GROUP BY ib.idea_id
An example in SQL Fiddle.
SELECT t1.*,sum(t2.thumbs=1) as up,sum(t2.thumbs=0) as down FROM table1 as t1 left join table2 as t2 using(idea_id) group by t1.idea_id;

SQL: Display multiple record values in one field from a subquery

I have an SQL LIKE:
SELECT S.*,
(SELECT I.NAME FROM institution I, inst_map IM
WHERE IM.STUDENT = S.ID AND IM.INSTITUTION = I.ID) as INSTITUTIONS
FROM student S
In this case it is possible for my subquery to return multiple records (I will get an error: Subquery returns more than 1 row).
How to show those multiple values from my subquery in one field (in my case INSTITUTIONS ) separated by commas?
All ideas are welcome.
Try this query -
SELECT s.*, GROUP_CONCAT(t.NAME) INSTITUTIONS FROM student s
LEFT JOIN (SELECT * FROM institution i
JOIN inst_map im
ON im.INSTITUTION = i.ID
) t
ON s.ID = t.STUDENT
GROUP BY s.ID
The GROUP_CONCAT function will help you to get values separated by commas.
DECLARE #List VARCHAR(5000)
SELECT #List = COALESCE(#List + ', ' + Display, Display)
FROM TestTable
Order By Display
query is taken from following link, and the article explain the query perfectly, I hope it works
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/289/creating-comma-separated-list-in-sql.aspx
P.S Its for SQL Server, i guess

Multiple GROUP_CONCAT on different fields using MySQL

I have a query like this:
SELECT product.id,
GROUP_CONCAT(image.id) AS images_id,
GROUP_CONCAT(image.title) AS images_title,
GROUP_CONCAT(facet.id) AS facets_id
...
GROUP BY product.id
And the query works, but not as expected, because if I have a product with 5 facets and 1 image (suppose an image with id=7), then I get something like this in "images_id":
"7,7,7,7,7"
If I have 2 images (7 and 3) then I get something like:
"7,7,7,7,7,3,3,3,3,3"
and in facets I get something like:
"8,7,6,5,4,8,7,6,5,4"
I think MySQL is making some type of union of the differents rows returned by the query, and then concatenating everything.
My expected result is (for the last example):
images_id = "7,3"
facets_id = "8,7,6,5,4"
I can obtain that using DISTINCT in the GROUP_CONCAT, but then I have another problem:
If I have two images with the same title, one of them is ommited, and then I get something like:
images_id = "7,3,5"
images_title = "Title7and3,Title5"
So I miss the relation between images_id and images_title.
Does somebody know if it's possible to make this query in MySQL?
Maybe I'm complicating everything without any real benefits.
I'm trying to execute only one query because performance, but now I'm not so sure if it's even faster to execute two queries (one for selecting the facets and another for the images for example).
Please explain what do you think is the best solution for this and why.
Thanks !
Just add DISTINCT.
Example:
GROUP_CONCAT(DISTINCT image.id) AS images_id
You'll need to get each group separately:
SELECT
p.id,
images_id,
images_title,
facets_id,
...
FROM PRODUCT p
JOIN (SELECT product.id, GROUP_CONCAT(image.id) AS images_id
FROM PRODUCT GROUP BY product.id) a on a.id = p.id
JOIN (SELECT product.id, GROUP_CONCAT(image.title) AS images_title
FROM PRODUCT GROUP BY product.id) b on b.id = p.id
JOIN (SELECT product.id, GROUP_CONCAT(facet.id) AS facets_id
FROM PRODUCT GROUP BY product.id) b on c.id = p.id
...
You can add just the DISTINCT keyword, you'll get your desire results.
SELECT tb_mod.*, tb_van.*,
GROUP_CONCAT(DISTINCT tb_voil.vt_id) AS voil,
GROUP_CONCAT(DISTINCT tb_other.oa_id) AS other,
GROUP_CONCAT(DISTINCT tb_ref.rp_id) AS referral
FROM cp_modules_record_tbl tb_mod
LEFT JOIN cp_vane_police_tbl tb_van ON tb_van.mr_id= tb_mod.id
LEFT JOIN cp_mod_voilt_tbl tb_voil ON tb_voil.mr_id= tb_mod.id
LEFT JOIN cp_mod_otheraction_tbl tb_other ON tb_other.mr_id= tb_mod.id
LEFT JOIN cp_mod_referral_tbl tb_ref ON tb_ref.mr_id= tb_mod.id
WHERE tb_mod.mod_type = 2 GROUP BY tb_mod.id
If the issue is speed, then it may be a lot faster to simply select all the data you need as separate rows, and do the grouping in the application, i.e.:
SELECT product.id, image.id, image.title, facet.id
Then in the application:
foreach row:
push product_id onto list_of_product_ids
push image_id onto list_of_image_ids
etc.

MySQL: How to subtract results of a JOIN from another query?

I need to exclude the results from a complex query from another query. I don't know how to make a LEFT JOIN work with the results of another JOIN query.
I want to return fields from lt.contacts after subtracting (excluding) the result of this:
(SELECT `contacts`.`idContacts` AS id, `contacts`.`First_Name`, `contacts`.`Last_Name`
FROM `lt`.`contacts`
JOIN `lt`.`groups`
JOIN `lt`.`groups_has_contacts`
ON Contacts_idContacts=idContacts
WHERE idGroup
IN (35)
AND Groups_idGroup
IN (35))
From the results of this:
SELECT * FROM `lt`.`groups_has_contacts` Where `Groups_idGroup` = 37)
I'm pulling my hair out -- any help before I am bald would be appreciated!
Try this:-
SELECT contacts.idContacts, contacts.First_Name, contacts.Last_Name
FROM contacts, groups
where contacts.idContacts=groups.idgroup
OR
SELECT contacts.idContacts, contacts.First_Name, contacts.Last_Name
FROM contacts INNER JOIN groups ON contacts.idContacts=groups.idgroup