(MySQL) Why are there no rows showing after a query? - mysql

I have a quick question about why rows are not showing up after a query. Here's a sample query I'm using based on 3 tables I have in my database.
select ff9characters.name, ff9apchart.apneeded
from ff9characters
join ff9apchart
on ff9apchart.charID=ff9characters.id
join ff9abilities
on ff9apchart.abilityid=ff9abilities.id
where ff9abilities.ability = 'Accuracy+'
group by ff9characters.name
order by ff9characters.id;
Nothing shows up at all. I would like to add that the ability column in ff9abilities is filled with data of the text datatype and has 'Accuracy+' listed.
If I change the WHERE statement to:
where ff9abilities.ability like 'Acc%'
All of the data shows up. I got 4 rows returned (which was my desired result).
Should I have changed the datatype for the ability column from text to varchar instead so that it reads all the data?

Could be you have improper white space try with trim and (lower)
select ff9characters.name, ff9apchart.apneeded
from ff9characters
join ff9apchart
on ff9apchart.charID=ff9characters.id
join ff9abilities
on ff9apchart.abilityid=ff9abilities.id
where trim(ff9abilities.ability) = 'Accuracy+'
group by ff9characters.name
order by ff9characters.id;

Related

How to sum all elements in an indexed table

I tried to make a query to my database with this structure: Data Base Structure
I did this query:
SELECT partido.acronimoPartido, SUM(votosacta.numVotos) FROM partido, votosacta WHERE votosacta.partido_idpartido=1 AND partido.idpartido=1
This query does work like I want but displays only the SUM of 'votos' for idpartido=1
I want to be able to sum numVotos from 'votosacta' table for each member of my 'partido' table indexed in 'votosacta' but I seem to not be able to get the right sintax.
I tried something like this:
SELECT partido.acronimoPartido, SUM(votosacta.numVotos) FROM partido, votosacta WHERE votosacta.partido_idpartido = partido.idpartido
You need a group by clause:
select p.acronimoPartido,
SUM(v.numVotos)
from partido p
join votosacta v on v.partido_idpartido = p.idpartido
group by p.acronimoPartido
Also, use explicit join syntax instead of old comma based syntax and use aliases to make your queries concise and readable.

SQL JOIN gives double results

My database and SQL:
http://sqlfiddle.com/#!9/ebddb/1/0
Problem:
It's returning duplicates, with the wrong data in the name-column, when there are less than 7 records in the notchtype-table
My Question:
Why does it return duplicates and how to prevent it?
Expected result:
This fiddle shows the expected result: http://sqlfiddle.com/#!9/22660/1
In this result the only thing more added than in my actual database and SQL are 2 records in the notchtype-table
So the id, notchid and number columns should be unique in the returned rows.
The screenshot in the answer of Piyush Gupta is showing the right expected result. The same query on SQL fiddle and locally on MariaDB version 10.1.9 are returning something different
Notes:
I found out that when there at least 7 records in the notchtype table, there are suddenly no duplicates anymore and the problem is 'solved'.
The null values should indeed be null.
The size-column is actually returning the right values, although the LEFT JOIN is more or less the same
The ID's in notches.notchdescr 'connects' with the ID's in notchtype.notchtypeid column and is returned as the name column in the fiddle
The ID's in notches.notchsize 'connects' with the ID's in notchsize.notchsizeid column and is returned as the size column in the fiddle
Not working:
INNER JOIN, don't know why
DISTINCT, because the name-columns have different values, so there not exact duplicates
GROUP BY, because it returns all the same values in the name-column
Update on answer/comments from Piyush Gupta
Query executed on MySQL 5.7:
SELECT
notches.id,
notches.notchid,
notches.number,
notches.xcoord,
notches.ycoord,
notches.mapid,
notches.location,
notches.date,
notches.price,
notches.invoiced,
notchsize.size AS notchsize,
notchtype.name AS notchdescr
FROM
notches
LEFT JOIN
notchtype ON
notches.notchdescr = notchtype.notchtypeid
LEFT JOIN
notchsize ON
notches.notchsize = notchsize.notchsizeid
WHERE
notches.del = 0
AND
notches.projectid = '2016032411364363055'
GROUP BY notches.id, notches.notchid, notches.number
ORDER BY notches.number ASC
Result:
SOLVED!
LEFT JOIN on VARCHAR = BIGINT field causes the strange returned values. See answer and comments of Piyush Gupta
You missed the GROUP BY in your query for Aggregate the data. so your query will be,
SELECT
notches.id,
notches.notchid,
notches.number,
notches.xcoord,
notches.ycoord,
notches.mapid,
notches.location,
notches.descr,
notches.date,
notches.price,
notches.invoiced,
notchtype.name AS notchdescr,
notchsize.size AS notchsize
FROM
notches
LEFT JOIN
notchtype ON
notches.notchdescr = notchtype.notchtypeid
LEFT JOIN
notchsize ON
notches.notchsize = notchsize.notchsizeid
WHERE
notches.del = 0
AND
notches.projectid = '2016032411364363055'
GROUP BY notches.id,
notches.notchid,
notches.number
ORDER BY notches.number ASC;
Output: ONLINE DEMO HERE
NOTE: I Imported your data structure locally and I'm getting same output which is your expectation but In SQLFiddle, notchtype.name AS notchdescr column is not executing in SQLFiddle that is showing only name column of notchtype table. So you can use above query and check locally in your database. I hope you will get require output.
Screenshot(Using MySQL Workbench)
Update 1: It was strange error. I reviewed database structure and found solution that was data type issue only. You were joining bigint and varchar data type so you need to correct data type. Here I'm changing data type bigint to varchar for notchsizeid in notchsize table and notchtypeid in notchtype table. Finally your Expected output is coming. You can SEE OUTPUT HERE.
You can do by using group by . But you need to tell your logic for this.
SELECT
notches.id,
notches.notchid,
notches.number,
notches.xcoord,
notches.ycoord,
notches.mapid,
notches.location,
notches.descr,
notches.date,
notches.price,
notches.invoiced,
notchtype.name AS notchdescr,
notchsize.size AS notchsize
FROM
notches
LEFT JOIN
notchtype ON
notches.notchdescr = notchtype.notchtypeid
LEFT JOIN
notchsize ON
notches.notchsize = notchsize.notchsizeid
WHERE
notches.del = 0
AND
notches.projectid = '2016032411364363055'
group by id
ORDER BY notches.number ASC

Group rows but keep values where not null

I am trying to group rows in MySQL but end up with a wrong result.
My DB looks like this:
I'm using this query:
SELECT
r_id, va_id,va_klasse,va_periode,
1va_mer,1va_hjem,1va_mot,1va_bil,1va_fit,1va_hand,1va_med,1va_fra,
2va_mer,2va_hjem,2va_trae,2va_bil,2va_sty,2va_mus,2va_med,2va_fra,
3va_mer,3va_hjem,3va_mot,3va_bil,3va_pima,3va_nat,3va_med,3va_fra,
va_lock, va_update
FROM o6hxd_valgfag
WHERE va_klasse IN('7A','7B','7C','8A','8B','8C','9A','9B','9C')
GROUP BY va_id
ORDER BY va_klasse,va_name
This produces a wrong result, where one row is returned with only the first three numbers 123 and not the ones from row two and three.
What I would like is a result where the numbers 123, 321 and 132 are gathered in one line.
I can explain more detailed if this isn't sufficient.
If across those fields there should only be ever one value, you should really have them all in the same record and go about fixing it to insert and update the same record.
Ie I am aware that you database isn't designed correctly
However
To dig you out, you could give this a crack, I suppose.
SELECT
r_id, va_id,va_klasse,va_periode,
MAX(1va_mer),MAX(1va_hjem),MAX(1va_mot),MAX(1va_bil),MAX(1va_fit),MAX(1va_hand),MAX(1va_med),MAX(1va_fra),
MAX(2va_mer),MAX(2va_hjem),MAX(2va_trae),MAX(2va_bil),MAX(2va_sty),MAX(2va_mus),MAX(2va_med),MAX(2va_fra),
MAX(3va_mer),MAX(3va_hjem),MAX(3va_mot),MAX(3va_bil),MAX(3va_pima),MAX(3va_nat),MAX(3va_med),MAX(3va_fra),
va_lock, va_update
FROM o6hxd_valgfag
WHERE va_klasse IN('7A','7B','7C','8A','8B','8C','9A','9B','9C')
GROUP BY va_id
ORDER BY va_klasse,va_name
Your query will not work as intended. Think about this use-case:
what if for row1 (r_id =9), the fields 2va_sty, 2va_mus, 2va_med are not empty and has values?
In such case what should your desired output be? It certainly cannot be the numbers 123, 321 and 132 gathered in one line. Group by is usually used if you want to use aggregate functions executed against a certain field value, in your case va_id.
Not a solution to your problem but i think a better query would be like this (because of the not named columns in the group by clause https://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html):
SELECT
aa.r_id, aa.va_id, aa.va_klasse, aa.va_periode,
aa.1va_mer, aa.1va_hjem, aa.1va_mot, aa.1va_bil, aa.1va_fit, aa.1va_hand, aa.1va_med, aa.1va_fra,
aa.2va_mer, aa.2va_hjem, aa.2va_trae, aa.2va_bil, aa.2va_sty,2va_mus, aa.2va_med, aa.2va_fra,
aa.3va_mer, aa.3va_hjem, aa.3va_mot, aa.3va_bil, aa.3va_pima, aa.3va_nat, aa.3va_med, aa.3va_fra,
aa.va_lock, aa.va_update
FROM o6hxd_valgfag AS aa
INNER JOIN (
SELECT va_id
FROM o6hxd_valgfag
GROUP BY va_id
) AS _aa
ON aa.va_id = _aa.va_id
WHERE aa.va_klasse IN ('7A','7B','7C','8A','8B','8C','9A','9B','9C')
ORDER BY aa.va_klasse, aa.va_name;

How to write update query using 2 tables and conditions

I am trying to update/revise the department names in a table called "DEPART_NAMES_AS_SUBMITTED" using another table called "DEPART_NAMES_REQUIRED." I would like this update to occur only if the line numbers in "DEPART_NAMES_AS_SUBMITTED" are within the line number range [LOW] [HIGH] in the second table called "DEPART_NAMES_REQUIRED." If the line number is less/more than the [LOW] [HIGH] range the department name should remain the same. I have unsuccessfully tried numerous SQLs including the following:
UPDATE DEPT_NAMES_SUBMITTED INNER JOIN DEPT_NAMES_REQUIRED ON(DEPT_NAMES_SUBMITTED.LINE_NUMBER = DEPT_NAMES_REQUIRED.HIGH) AND (DEPT_NAMES_SUBMITTED.LINE_NUMBER = DEPT_NAMES_REQUIRED.LOW) SET DEPT_NAMES_SUBMITTED.DEPART_NAME = [DEPT_NAMES_REQUIRED].[DEPART_NAME]
WHERE (((DEPT_NAMES_REQUIRED.LINE_NUMBER) Between [low] And [high]));
Thank you for taking the time to read and answer this question.
I think your query is fine if you remove the square braces and put in the right logic:
UPDATE DEPT_NAMES_SUBMITTED ns INNER JOIN
DEPT_NAMES_REQUIRED hr
ON ns.LINE_NUMBER <= nr.HIGH AND
ns.LINE_NUMBER >= nr.LOW
SET ns.DEPART_NAME = nr.DEPART_NAME;
Notice that table aliases make the query easier to write and to read.

MySQL Locate Duplicates between two table with similar column

Using this question's answer. I'm trying to find duplicate records between two tables by the column names matrix_unique_id and Matrix_Unique_ID in each table and then display the full address. The Full address columns are formatted differently from each other in each table so I cannot use that as a comparison. I'm getting an "unknown column fort_property_res.matrix_unique_id" error but everything looks okay?
So two questions:
Will this query find duplicates correctly?
Why the unknown column error?
SQL query:
SELECT matrix_unique_id, full_address
FROM fort_property_res
INNER JOIN (
SELECT Matrix_Unique_ID, FullAddress
FROM sunshinemls_property_res
GROUP BY FullAddress
HAVING count(fort_property_res.matrix_unique_id) > 1
) dup ON fort_property_res.matrix_unique = sunshinemls_property_res.Matrix_Unique_ID
The solution you're trying to copy is a totally different case. You have two tables and (it looks like) a convenient matrix_unique_id to join on, so this is much easier:
SELECT fort.matrix_unique_id, fort.full_address AS fortAddress, sun.FullAddress AS sunAddress
FROM fort_property_res fort, sunshinemls_property_res sun
WHERE fort.matrix_unique_id = sun.Matrix_Unique_ID