i have this database
i want to select options_e if it has options less than 4, or something like this
SELECT * FROM `options` WHERE count(qid) <4
so that it will return the result of options who are less than 4. but on phpmyadmin when i run the query, it says invalid use of group function. why do I get this error? who can i fix this?
You can try like this:
SELECT options_e, options_f, options_p, options_a, options_s FROM `options`
GROUP BY options_e, options_f, options_p, options_a, options_s
HAVING count(qid) < 4
Related
When querying my MySQL database, I use the line
SELECT `playlistDate` from `tracks` GROUP BY `playlistDate`
The JSON-ified result statement is this
[{"playlistDate":"2020-09-14T05:00:00.000Z"},{"playlistDate":"2020-09-21T05:00:00.000Z"},
{"playlistDate":"2020-09-28T05:00:00.000Z"},{"playlistDate":"2020-10-05T05:00:00.000Z"},
{"playlistDate":"2020-10-12T05:00:00.000Z"},{"playlistDate":"2020-10-19T05:00:00.000Z"},
{"playlistDate":"2020-10-26T05:00:00.000Z"},{"playlistDate":"2020-11-02T06:00:00.000Z"}
I want it to look like this, without the timestamps
[{"playlistDate":"2020-09-14"},{"playlistDate":"2020-09-21"},
{"playlistDate":"2020-09-28"},{"playlistDate":"2020-10-05"},
{"playlistDate":"2020-10-12"},{"playlistDate":"2020-10-19"},
{"playlistDate":"2020-10-26"},{"playlistDate":"2020-11-02"}
I've tried changing my query to either of these with no luck
SELECT DATE(`playlistDate` from `tracks` GROUP BY `playlistDate`)
SELECT DATE(`playlistDate`) from `tracks` GROUP BY `playlistDate`
How can I use the DATE() function to get the results I am expecting?
The ugly way to do it is using substr(playlistDate, 1, 10).
You also can use DATE_FORMAT(playlistDate,'%y-%m-%d')
Assuming playlistDate is a string, you can use STR_TO_DATE.
SELECT distinct(STR_TO_DATE(playlistDate,'%Y/%m/%d')) from tracks
If its a date:
SELECT distinct(DATE_FORMAT(playlistDate,'%Y/%m/%d')) from tracks
I just wanted to simply count using where conditional and yet this query ask me for parameter instead of automatically execute the query
SELECT COUNT(ActDiscDischargingPort) from(
SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable) WHERE SchLoadVessel LIKE "XB24 - MV. MEMPHIS" AND SchLoadVoyageNo LIKE "0019";
What is the proper way of writing this query?
Found the answer, turn out the query has to be like this
SELECT COUNT(ActDiscDischargingPort) from ( SELECT DISTINCT ActDiscDischargingPort FROM SelisihLoadVSActualLoadTable WHERE SchLoadVoyageNo LIKE "XB24 - MV. MEMPHIS" AND SchLoadVessel LIKE "0019" )
Every derived table must have its own alias.
The correct syntax would be
SELECT
COUNT(ActDiscDischargingPort)
from(
SELECT
DISTINCT ActDiscDischargingPort
FROM
SelisihLoadVSActualLoadTable
) AS T
WHERE
SchLoadVessel LIKE "XB24 - MV. MEMPHIS"
AND SchLoadVoyageNo LIKE "0019";
You can further speed up your query by optimizing it a bit.
I have a mysql table contains lot's of records. my table has a varchar field and a timestamp field. (I have one record for every minute)
I want to select records like this:
1,3,5,7,9,11,...
or 1,4,7,10,13,..
or something like this.
I can get done it using php while function, but it is not a good solution. is there any mysql select parameter to get it exactly from mysql?
p.s: sorry for post title, this is the only title stackoverflow accept it.
select * from table where identity_column %2 <>0 -- to select 1,3,5,7,9...
and for your 2 condition do this !
select * from table where identity_column%3 =1 -- to select 1,4,7,10,13,....
For selecting records like 1,3,5,7,9,11,etc. You can do this:
SELECT *
FROM TableName
WHERE autoIncreamentField % 2
NB: Not necessary to check where clause against 0 or 1. It will select records if where clause returns 1. An example in Fiddle.
For records like 1,4,7,10,13,etc. You can do:
SELECT *
FROM TableName
WHERE (autoIncreamentField % 3)=1
select * from table order by rand()
Problematic Query:
SELECT * FROM my_links WHERE taxonomy='ait-dir-item-category'
Above query returns no record and no error.
I am getting empty result, but I have exactly same value 'ait-dir-item-category' for taxonomy column.
But following query returns result
Perfect Query:
SELECT * FROM my_links WHERE taxonomy='post_tag'
Please tell me what is the problem.
Try with LIKE statment
SELECT * FROM my_links WHERE taxonomy LIKE '%ait-dir-item-category%'
Did you tried using like statement
Can you try like this,
select * from my_link where taxonomy like '%ait-dir-item%'
Trying to get a check sum of results of a SELECT statement, tried this
SELECT sum(crc32(column_one))
FROM database.table;
Which worked, but this did not work:
SELECT CONCAT(sum(crc32(column_one)),sum(crc32(column_two)))
FROM database.table;
Open to suggestions, main idea is to get a valid checksum for the SUM of the results of rows and columns from a SELECT statement.
The problem is that CONCAT and SUM are not compatible in this format.
CONCAT is designed to run once per row in your result set on the arguments as defined by that row.
SUM is an aggregate function, designed to run on a full result set.
CRC32 is of the same class of functions as CONCAT.
So, you've got functions nested in a way that just don't play nicely together.
You could try:
SELECT CONCAT(
(SELECT sum(crc32(column_one)) FROM database.table),
(SELECT sum(crc32(column_two)) FROM database.table)
);
or
SELECT sum(crc32(column_one)), sum(crc32(column_two))
FROM database.table;
and concatenate them with your client language.
SELECT SUM(CRC32(CONCAT(column_one, column_two)))
FROM database.table;
or
SELECT SUM(CRC32(column_one) + CRC32(column_two))
FROM database.table;