I want to retrieve text data in the form of numbers from MySQL, but the query that I made actually creates duplicate data,
SELECT a.* FROM users a
LEFT JOIN projects b
ON CONCAT(',',b.team,',') LIKE CONCAT('%',a.id,'%')
WHERE b.id = ${params.project_id}
GROUP BY a.id)
For example: mysql 1,13 instead appears 1,1,13 every two digits then appears duplicate
Related
SELECT *
from projects
where projects.project_code in (select distinct_code from only_project_code where distinct_code is not null)
There are 84 matching values between distinct_code and project_code. But this query is returning only 11. Why?
Tables are here. Notice the query run on them.
That because of table projects may contains unique values, if you want duplicate you need a JOIN :
select p.project_code, pc.distinct_code
from projects p inner join -- you may need LEFT JOIN instead
only_project_code pc
on pc.distinct_code = p.project_code;
I'm not sure if this is possible exactly as stated in the title, but what I'm trying to accomplish, however possible, is what would amount to left joining two columns from one table, each on a separate table.
Here's the statement I'm working with. Unfortunately, MySQL doesn't allow me to actually do this and gives me an error message. Is there another way to accomplish what I want? Is my syntax perhaps just off?
select h.HITTER_ID, u.UMPIRE_ID, u.HAND_B
from
hitters h,
schedules sched
LEFT JOIN
umpires u
ON sched.home_base_umpire_id = u.UMPIRE_ID and h.HAND_B = u.HAND_B
In the umpires table, each UMPIRE_ID appears twice, once with HAND_B = "R" and once with HAND_B = "L"
Essentially, I want to:
1) Pull the UMPIRE_ID from umpires when that UMPIRE_ID appears in schedules
2) Of the two UMPIRE_ID records, select the one with the HAND_B field that corresponds to the HAND_B field in hitters
I could put the "h.HAND_B = u.HAND_B" in the where clause, but that would require that the UMPIRE_ID not be NULL, and I need to leave open the possibility that it is NULL.
How can I accomplish this?
You need to modify your query to be like below
select h.HITTER_ID,
u.UMPIRE_ID,
u.HAND_B
from hitters h
left join umpires u on h.HAND_B = u.HAND_B
left join schedules sched ON sched.home_base_umpire_id = u.UMPIRE_ID;
I have two tables: users and tabletop_questions. The users table has a column called institution_primary_function and tabletop_questions has a column called target_institutions. An example value for users.institution_primary_function = C and an example value for tabletop_questions.target_institutions = A,B,C,D,E,F.
I am trying to return only those rows where the value for users.institution_primary_function is contained in tabletop_questions.target_institutions (comma delimited list) using the below query.
SELECT * FROM tabletop_questions
LEFT JOIN users
ON users.institution_primary_function
LIKE CONCAT( '%,', tabletop_questions.target_institutions, ',%' )
However, with this query, every row from the tabletop_questions table is returned with all the values from the joined users table as NULL. Could you please advise me as to where I am going wrong?
You should normalize your data because searching through a comma-delimited string is not efficient. Yet, MySQL includes a function for this called FIND_IN_SET(). Use an INNER JOIN instead of a LEFT OUTER JOIN to only return matching records.
SELECT * FROM tabletop_questions
INNER JOIN users
ON FIND_IN_SET(users.institution_primary_function,
tabletop_questions.target_institutions)
SELECT submissions.created_at, guests.accepted
FROM submission
INNER JOIN guests
ON guests.event_id = 1
ORDER BY submissions.created_at ASC;
Currently this query is taking a long time to run (may not even return everything).
What I am trying to accomplish:
Grab the field submissions.created at and guests.accepted (the two are linked by a submission_id)
given a certain event_id (event 1 for example).
What is wrong with my query?
You forgot to give the JOIN condition in your query. Try this:
SELECT submissions.created_at, guests.accepted
FROM submission s
INNER JOIN guests g on g.event_id = s.submissions_id
where guests.event_id = 1
ORDER BY submissions.created_at ASC;
SELECT submissions.created_at, guests.accepted
FROM submission
INNER JOIN guests
ON guests.(column to match against) = submissions.(column to match on)
where guests.event_id=1
ORDER BY submissions.created_at ASC;
As many others here have already said, your join is a little goofed. It's attempting to join the one row that matches in the guests table against every single row in the submission column.
You would need to do your join like this :
Inner join guest on guest.event_id = submissions_id
where guest.event_id = 1
When you join you need to join on two columns from different table (most of the time id columns)
You can read the MySQL example for more explanation. And here is the link to the MySQL reference manual
I have three tables, table one (tableA) containing users data like name and email, another (tableB) flagging if they wish to receive an email or sms notice and a last table (tableC) noting which type of notices they require out of four types.
tableA and tableB have single rows for each user, but tableC could have up to 4 entries.
Is it possible I can concatinate the multiple entries of tableC in to a single row return, additionally with my other data from my other two tables. Perhaps building some sort of comma delimited field in the row?
I currently have this as my mySQL query:
SELECT
ppf.page_id,
prv.`name`,
prv.surname,
prv.email,
prv.mobile,
ppf.email,
ppf.sms,
page_profile_noticetypes.noticetype
FROM
page_registration_value AS prv
Inner Join page_profile_value AS ppf ON prv.page_id = ppf.user
Inner Join page_profile_noticetypes ON page_profile_noticetypes.page_id = ppf.page_id
WHERE ppf.sms = 1 OR ppf.email = 1
#itsadok answered it - Result:
SELECT
ppf.page_id,
prv.`name`,
prv.surname,
prv.email,
prv.mobile,
ppf.email,
ppf.sms,
GROUP_CONCAT(page_profile_noticetypes.noticetype)
FROM
page_registration_value AS prv
Inner Join page_profile_value AS ppf ON prv.page_id = ppf.user
Inner Join page_profile_noticetypes ON page_profile_noticetypes.page_id = ppf.page_id
WHERE ppf.sms = 1 OR ppf.email = 1
GROUP BY prv.email