SQL Query Not Working - Not Picking Up Matching Rows - mysql

Ive been trying this query for half an hour - for some reason it only picks up the first row when I know that there are other rows that match the criteria. Any thoughts? Thanks.
SELECT `res`.id, `res`.time, `res`.price, `res`.ppl, `res`.rest, `city`.city
FROM `res`
JOIN `city` ON `res`.city = `city`.id
WHERE `res`.id > '0'
LIMIT 0 , 2

The data and the join don't match. There is only one city that will be returned with your query - joining res with city: city 6. and with that city there's only one res returned.

Related

MySQL Get One Column Only Once

I have 2 MySQL tables in which I get data from in one query
"tables" table:
"checks" table:
The query I have been trying and the result of it:
SELECT tables.tableName, tables.tableRes, tables.tableResFor, checks.chkID, checks.chkDate, checks.chkStatus
FROM tables
LEFT JOIN checks ON tables.tableID=checks.chkTable
WHERE tables.tableID=3
ORDER BY checks.chkStatus DESC, checks.chkID ASC
Here are the problems
If there were no results from the query, I need the tableName column which comes out never null, so other columns can be null (works now)
I don't want to get the rows after first row, if the chkStatus column is 1 or 0 or null, shortly I need the rows with 2 on chkStatus, if the first row is 0, 1 or null, I don't need the other rows...
Thanks in advance, I have been working on this problem for more than 10 hours...
I need the other checks where chkStatus is 2 so, add the condition to the join
SELECT
tables.tableName
, tables.tableRes
, tables.tableResFor
, checks.chkID
, checks.chkDate
, checks.chkStatus
FROM tables
LEFT JOIN checks ON tables.tableID = checks.chkTable AND chkStatus = 2
WHERE tables.tableID = 3

MySQL SELECT query that counts left joined rows takes too long

Does anyone know how to optimize this query?
SELECT planbook.*,
COUNT(pb_unit_id) AS total_units,
COUNT(pb_lsn_id) AS total_lessons
FROM planbook
LEFT JOIN planbook_unit ON pb_unit_pb_id = pb_id
LEFT JOIN planbook_lesson ON pb_lsn_pb_id = pb_id
WHERE pb_site_id = 1
GROUP BY pb_id
The slow part is getting the total number of matching units and lessons. I have indexes on the following fields (and others):
planbook.pb_id
planbook_unit.pb_unit_pb_id
planbook_lesson.pb_lsn_pb_id
My only objective is to get the total number of matching units and lessons along with the details of each planbook row.
However, this query is taking around 35 seconds. I have 1625 records in planbook, 13,693 records in planbook_unit, and 122,950 records in planbook_lesson.
Any suggestions?
Edit: Explain Results
SELECT planbook.*,
( SELECT COUNT(*) FROM planbook_unit
WHERE pb_unit_pb_id = planbook.pb_id ) AS total_units,
( SELECT COUNT(*) FROM planbook_lesson
WHERE pb_lsn_pb_id = planbook.pb_id ) AS total_lessons
FROM planbook
WHERE pb_site_id = 1
planbook: INDEX(pb_site_id)
planbook_unit: INDEX(pb_unit_pb_id)
planbook_lesson: INDEX(pb_lsn_pb_id)
Looking to your query
You should add and index for
table planbook column pb_site_id
and eventually a composite one for
table planbook column (pb_site_id, pd_id)

Should MAX in Group By Retrieve the Whole Row Data

I have a bunch of sql columns to retrieve. This is my SQL statement:
SELECT b.iposx_model_id,
a.mrModel, MAX(a.mrRevision) as mrRevision, a.mrApprovedBy, ...
FROM mydb1.tbl_model_revision a
INNER JOIN mydb2.model_from_revision_process b
ON b.mrModel = a.mrModel
WHERE a.mrType = 1
GROUP BY a.mrModel
ORDER BY b.iposx_model_id ASC
On a certain mrModel column taht I retrieved, these are the data that I'm querying for:
In my query, it states that I should get the data with the Max mrRevision which corresponds to the third row.
However, upon running the sql statement, I got the max mrRevision which is 2, coming from the third row. But the other column data I get came from the 2nd row, an example is mrApprovedBy which is 1035 instead of 10. Why is that happening?
The use group by for only a part of the column not involved in aggegation function is select clause
is depreacted in SQL because where work produce unpredicatble result
In most DB and in the most recent version of mysql this use of group by is not allowed
for obtain the row corresponding to the max value you could use a join on the values that match the right content
SELECT
b.iposx_model_id
, a.mrModel
, t.mrRevision
, a.mrApprovedBy
, ...
FROM mydb1.tbl_model_revision a
JOIN (
select mrModel, MAX(mrRevision) as mrRevision
from mydb1.tbl_model_revision
GROUP BY a.mrModel
) t on a.mrModel = t.mrModel and a.mrRevision = t.mrRevision
JOIN mydb2.model_from_revision_process b ON b.mrModel = a.mrModel
WHERE a.mrType = 1
ORDER BY b.iposx_model_id ASC

Select last two values from two IDs

I would like to select two specific values, the first value is the last inserted row where the ID_SENSOR is 1, and the second value is the last inserted row where the ID_SENSOR is 2.
My Database table:
My Query:
SELECT DATA FROM (SELECT * FROM registovalores WHERE ID_SENSOR = '1' OR ID_SENSOR = '2' ORDER BY ID_SENSOR DESC LIMIT 2) as r ORDER BY TIMESTAMP
My Query is printing the last value just from the ID_SENSOR 1, which it means that I'm only getting the last inserted values, and not the last inserted value from both IDS.
I would like to print my values like this:
ID_SENSOR 1 = 90
ID SENSOR 2 = 800
What do I need to change on my Query?
Thank you.
One method uses a correlated subquery:
SELECT rv.*
FROM registovalores rv
WHERE rv.ID_SENSOR IN (1, 2) AND
rv.TIMESTAMP = (SELECT MAX(rv2.TIMESTAMP)
FROM registovalores rv2
WHERE rv.ID_SENSOR = rv2.ID_SENSOR
);
You have to have two separate queries, one per sensor.
select id_sensor, data
from the_table
where id_sensor = 'sensor_1'
order by timestamp desc -- the latest value is the first to come
limit 1; -- only pick the top (latest) row.
If you want to query for more than one value in a single database roundtrip, consider using union all between several such queries.
Please note that such a query may return one row or zero rows, since data for a particular sensor may not be available yet.

The query gives single row query returns more than one row

I'm trying to show staff_code, staff_name and dept_name for those who have taken one book.
Here's my query:
SELECT SM.STAFF_CODE,SM.STAFF_NAME,DM.DEPT_NAME,BT.BOOK_CODE
FROM STAFF_MASTER SM,DEPARTMENT_MASTER DM,BOOK_TRANSACTIONS BT
WHERE SM.DEPT_CODE =DM.DEPT_CODE
AND SM.STAFF_CODE = (
SELECT STAFF_CODE
FROM BOOK_TRANSACTIONS
HAVING COUNT(*) > 1
GROUP BY STAFF_CODE)
It gives the error:
single-row subquery returns more than one row.
How to solve this?
Change = to IN:
WHERE SM.STAFF_CODE IN (SELECT ...)
Because the select returns multiple values, using equals won't work, but IN returns true if any of the values in a list match. The list can be a hard-coded CSV list, or a select with one column like your query is.
That will fix the error, but you also need to remove BOOK_TRANSACTIONS from the table list and remove BOOK_CODE from the select list.
After making these changes, your query would look like this:
SELECT SM.STAFF_CODE,SM.STAFF_NAME,DM.DEPT_NAME
FROM STAFF_MASTER SM,DEPARTMENT_MASTER DM
WHERE SM.DEPT_CODE =DM.DEPT_CODE
AND SM.STAFF_CODE IN (
SELECT STAFF_CODE
FROM BOOK_TRANSACTIONS
HAVING COUNT(*) > 1
GROUP BY STAFF_CODE)
I recommend learning the modern (now over 25 year old) JOIN syntax.