I'm trying to check with a query if there are two different users in a giveaway.
How could I do this?
Row
1 - $250.00 - NY
2 - $200.00 - DC
I'd like to somehow be able to make the query check for every row and check if there are two different users in the table. Since there is User 1 and User 2 in this table it should then return 2 because there are two different users in the database, if there are 3 return 3 etc.
Is there any syntax I could use for this, or any idea on how this could be done?
I don't know your table structure but use DISTINCT and it will return a list of unique values (one name of each).
SELECT DISTINCT name FROM users
If you wanted to count the number of entries then alter your statement to
SELECT COUNT(DISTINCT name) FROM users
Count() function provides the number of rows in a table.
https://www.w3schools.com/sql/sql_count_avg_sum.asp
SELECT COUNT(*) FROM TABLE_NAME;
Related
im actually a frontend developer not a backend developer. so believe me, its really hard for me to work with postgreSQL and i searched a lot for my question but i couldnt find exactly what i want. i want to create a query in supabase that returns all rows of 3 tables. my goal is to search in these rows for search results base on users entered text in my app. so i want to return all rows from 3 tables in a function and a request. then by using the methods of supabase js library i can filter the rows. you can understand my point better with this code example:
create
or replace function search()
returns /* what type of data should i return? */
language sql as $$
/*
* select all rows of table artists -> select all rows of table musics -> select all rows of
* table playlists
*/
$$;
then i can do something like this in js:
let query = supabase.rpc('search').ilike('name', search_text)
so how can i do something like this?
thanks for helping.
UPDATE
here are the rows with some of theie columns that are in my 3 tables:
note: the rows of my 3 tables are totally different from each other.
a row of table *musics*:
id | name | singer | listenTimes | link
1|without me|eminem|10000|www.test.com
a row of table *artists*:
id | name | monthlyListens | bio
1|eminem|123431|{bio infos}
a row of table *playlists*:
id | name | musicsId | musics | cover
1|your favorite playlist|[1,2,3]|3|www.test.com/cover.png
base on jiri baums answer, i want to create a function that has an argument. what this function does is to check the columns of rows from 3 tables to check if the argument is something like the value of a column and returns the rows that at least have a column like the value of argument. for example: the function has an argument with the value of emvnem. the rows 1 and 2 in the exampla above will be returned. because emvnem is like eminem. if its is exactly like eminem, the returned rows are row 1 and 2. so how can i implement something like this?
As a general rule, this would be a poor design:
In terms of efficiency, it's best to push filtering as close as possible to the data; PostgreSQL has ilike functionality built in, indeed the queries in supabase are based on SQL syntax. See the PostgreSQL docs for LIKE and ILIKE
SELECT *
FROM table_name
WHERE name ILIKE 'search text'
With any real amount of data, sending everything to the front-end for filtering will be prohibitive.
In terms of access to data, if you send the whole table to the front end, the user will be able to copy it, save it, do other things with it; in most cases, you probably prefer to avoid that.
If you nevertheless want to do this, the operator you're looking for is UNION; the PostgreSQL docs have an example of doing pretty much exactly this:
SELECT distributors.name
FROM distributors
WHERE distributors.name LIKE 'W%'
UNION
SELECT actors.name
FROM actors
WHERE actors.name LIKE 'W%';
Link: https://www.postgresql.org/docs/14/sql-select.html
I have a question about how to have the pagination work in server side having mysql as database. I have figured most of it out and it works perfectly fine but i was thinking is there any way to have the count query eliminated. Let's say user searches for the word jack and the results count are 25 paginated by 10 per page. my paginator needs to know the amount of all rows to show correct amount in paginator. So the way i am doing it now is first i use a sql query like this to have the count of all the rows which correspond to that criteria:
"SELECT COUNT(id) AS count FROM " + table + query
Then i do another query against database like this that uses LIMIT and OFFSET options to have that exact page:
"SELECT * FROM " + table + query + " LIMIT ? OFFSET ?"
Then i return two objects to client. first the count of all rows and the seconds the rows user needs to see now. My question is is this the most efficient way to do this or is there any better way to do it too?
You can achieve this with one query, but it will have burden on outputted data i.e. if you limit 1000 records for example, then total_records will show the number 1000 times with all rows in the result set. But at the same time, it will reduce 1 query:
SELECT
column1,
column2,
column3,
(SELECT COUNT(id) FROM `table`) AS total_records
FROM
`table`
LIMIT 0, 10
I didn't see anything wrong with your approach (although you can send the query to database in one trip). With the traditional way of pagination in database, you must know the total records, so it's just how to get the count.
improvements are mostly to do it in a different way.
Improvement 1: infinite scroll, this is get ride of pagination. May not be what you wanted, but we are seeing more and more website adopting this way. Does the user really need to know how many pages for a free text search?
Improvement 2: use ElasticSearch, instead of database. It's built for free text search and will definitely perform better than database. You can also get count (hits) and pages in one search request.
I am new at pentaho, I am using a step „Merge rows (diff)“ and compare two tables. Problem ist that I dont know the key and value fields to compare of my origin tables, I can only read them in „Javascript“-step. Do you know any variants how to use such parameters in „Merge rows (diff)“-Step? Especially I am interested in „Values to compare“, because I need two compare all columns in these two tables and the structure of the tables (for example column names) can change in database any time, so I will have always different number of fields in „value to compare“.
Thank you for your help.
So as we know firebase won't let order by multiple childs. I'm looking for a solution to filter my data so at the end I will be able to limit it to 1 only. So if I won't to get the lowest price it will be something like that:
ref.orderByChild("price").limitToFirst(1).on...
The problem is that I also need to filter it by dates (timestamp)
so for that only I will do:
.orderByChild("timestamp").startAt(startValue).endAt(endValue).on...
So for now that's my query and then I'm running on all results and checking for that one row that has the lowest price. my Data is pretty big and contains around 100,000 rows. I can changed it however I want.
for the first query that gets the lowest price but all timestamps causes that the returned row might be the lowest price but not in my dates range. However this query takes ONLY 2 seconds compared to the second one which takes 20 including my code to get the lowest price.
So, what are your suggestions on how to do it best? I know I can make another index which contains the timestamp and the price but those are different data values and it makes it impossible.
full data structure:
country
store
item
price,
timestamp
just to make it even more clear, I have 2 inner loops which runs over all countries and then over all stores. so the real query is something like that:
ref.child(country[i]).child(store[j]).orderByChild("timestamp").startAt(startValue).endAt(endValue).on...
Thanks!
I know this can be done easily with javascript/php, but I'm wondering if this is possible with pure MYSQL and one query.
So imagine this:
some rows are inserted with a number, that user has typed in. (for example,5,7,1 and 2)
So the row would now look like this:
I have one more table which contains user's data:
Now: is it possible to increase user's points by 10 if the number he had placed is above 5?
Thank you for help,
Nedas
Join the tables to find the corresponding rows, and then update them.
UPDATE points_table AS p
JOIN numbers_table AS n ON p.user = n.user
SET p.points = p.points + 10
WHERE n.number > 5