I am trying to do a filter query by using the next statement:
SELECT * FROM user_jobs,users WHERE user_jobs.job_title LIKE "%some_keyword%" **OR** user_jobs.job_title LIKE "%another_keyword%" AND user.id=user_jobs.userid
Specs: users.id is PK and user_jobs.userid is FK to users.id
I am trying to filter the users to get the ones that have similar values as specified. When I run it I get a very long loop and finally a large list of users that contains duplicates. (e.g. I only have 300 users and the query shows over 3000 results)
What am I doing wrong, please?
Thanks in advance!
AND takes precedence over OR; use parentheses to achieve the desired result.
SELECT * FROM user_jobs, users
WHERE
(user_jobs.job_title LIKE "%some_keyword%"
OR user_jobs.job_title LIKE "%another_keyword%")
AND users.id = user_jobs.userid
You need to use parentheses in that query.
SELECT * FROM user_jobs,users WHERE user.id=user_jobs.userid
AND (user_jobs.job_title LIKE "%some_keyword%"
OR user_jobs.job_title LIKE "%another_keyword%")
First off, the AND operator holds precedence in this case. Isolate your logic like so:
SELECT * FROM user_jobs,users WHERE (user_jobs.job_title LIKE "%some_keyword%" OR user_jobs.job_title LIKE "%another_keyword%") AND user.id=user_jobs.userid
Second of all, don't use SELECT * FROM .... This selects all your data, adding network overhead and taking up more time to transfer it all across the server.
Reference: https://dev.mysql.com/doc/refman/5.0/en/func-op-summary-ref.html
Even though you table contains 300 records it will result around 3000 records because you are selecting columns from both the tables but not giving any join condition in your query , so it will CROSS JOIN both the tables.
and for finding the `JOB_TITLE patter you can use Regular Expressions also as
SELECT * FROM USER_JOBS T1,USERS T2 WHERE REGEXP_LIKE(USER_JOBS.JOB_TITLE ,'SOME_KEYWORD|OTHER_KEYWORD') AND T2.ID=T1.USERID;
Related
I am new to mySQL.
I am following Mosh's tutorial to familiarize myself to SQL.
Here's my question for the following code.
SELECT *
FROM order_items
WHERE order_id = 6 AND unit_price*quantity > 30
When I looked up about SELECT *, it says: * means to return all all columns of the queried tables. Then I think SELECT * means that it grabs all tables from all schema.
My question is: Isn't it a bit inefficient and confusing to return all column provided my understanding is right? If the database become bigger and bigger, it will consume unnecessary effort to look up the keyword, so I think SELECT should specify what table it is referring to. Thanks for reading! 🥰
SELECT * does not fetch all tables from all schema. It only fetches the columns from the table you reference in your FROM clause. It only fetches the rows that match your WHERE clause.
The mistake is understandable given this statement in the MySQL documentation:
A select list consisting only of a single unqualified * can be used as shorthand to select all columns from all tables:
SELECT * FROM t1 INNER JOIN t2 ...
What they mean by "all tables" is only all tables referenced in this query. And only those in FROM or JOIN clauses. Not all tables everywhere.
I have what I believe to be a pretty unique use case. I would like to be able to runs a single SELECT statement on a database where I get one column from four tables. I need to run where clauses on each different table where I have one main clause that will be across each of the tables and I am not able to JOIN because the data in each column will be a different length and I don't want to have duplicate items.
I have an example of the Select statement below. Also I understand if this is not possible.
SELECT s.service_id, u.id AS "user_id", h.mac_address, l.id AS "location_id" FROM services s
LEFT JOIN db.av_product ap ON s.product_id = ap.id
WHERE s.customer_code LIKE 'test_customer'
AND u.customer_code LIKE 'test_customer'
AND h.customer_code LIKE 'test_customer'
AND l.customer_code LIKE 'test_customer'
AND s.parent_id IS NULL
AND s.active=0
AND ap.sku NOT REGEXP 'fakeregex'
AND l.active = "1"
AND h.hardware_id NOT IN ('44','45')
AND (u.support_user != 1 OR u.support_user IS NULL);
TIA!
You will need to use joins for your tables to make a single query OR you can try multiple queries merged with UNION keyword.
If you want to make a single query, have a look about SELECT DISTINCT or GROUP BY for handling duplicates.
wut up?
do you know what UNION is?
The UNION operator is used to combine the result-set of two or more SELECT statements.
but every SELECT statement within UNION must have the same number of columns; so there we got a problem.
you can handle it with WHERE operator so I won't get in to it.
anyway, UNION!
shall we?
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
anyway; your solution is UNION, maybe not like what I wrote.
you can try this link too.
https://www.w3schools.com/mysql/mysql_union.asp
have a clean code
I have a given list of patient IDs which I want to interact in, something like this:
SELECT * FROM (LIST OF IDs) WHERE ....
is it possible to have something similar to this using mysql?
it is feasible using IN in WHERE section
SELECT * FROM <table> WHERE <field> IN (<your list of ids comma separated>)
Note that this is valid up to 10 ids. With more the performance of the query will definitively decline and there are different approaches (temporary tables to join). But in that case I'd review your database schema to see if this is the correct approach
I would definitely recommend to use a subquery in WHERE id IN (...), just like this:
SELECT *
FROM table1
WHERE id IN (SELECT id
FROM table1
WHERE conditions... )
With this, you can add more conditions in both WHERE's and performance will be fine.
From my debian terminal I try to execute in mysql client a query like:
SELECT *
FROM stop_times_lazio_set2_withtime2 AS B
WHERE EXISTS
(SELECT *
FROM stop_times_lazio_set2_emptytime2 AS A
WHERE B.trip_id=A.trip_id);
table A contains around 3 million records.
table B is a sub set of A of around 400000 records.
I'd like to select every records of A thats have a row "parent" with the same id (yes its not an unique/primary id)
Now it takes more than hours...now I'm around 2h and i still seen just a blinking pointer... is it the query correct? Even I can't access to others mysql client like phpmyadmin.
There is any way to speed up the process?
There is a way to check how many records are processed at running times?
I guess you have already indexed trip_id? There is another way writing the query, maybe it helps:
SELECT *
FROM stop_times_lazio_set2_withtime
WHERE trip_id IN (SELECT trip_id FROM stop_times_lazio_set2_emptytime2)
I would expect a straight JOIN to be much much faster...
SELECT B.*
FROM stop_times_lazio_set2_withtime2 AS B
JOIN stop_times_lazio_set2_emptytime2 AS A ON B.trip_id=A.trip_id
Why not using a simpler request ?
SELECT A.*
FROM stop_times_lazio_set2_emptytime2 AS A, stop_times_lazio_set_withtime2 AS B
WHERE B.trip_id=A.trip_id;
With that many records, it will obviously take time.
You can actually prevent it by processing only a few at a time by adding this at the end of the request
LIMIT <beginning>, <number of records>
Did you tried "left join"??
sample:
select columns from withtime
left join emptytime on withtime.tripid=emptytime.tripid;
I have two simple querys, but i need a little help to join them in one result
$query="SELECT * FROM USLUGE WHERE marka='1'";
And second query
$query2="SELECT * FROM USLUGE WHERE marka='2'";
I know the result from $query will give ALL from table USLUGE where marka=1, and result $query2 will give me ALL from table USLUGE wwhere marka=2
But i need to join this two querys in one query, and when i do a WHILE loop, to give me all result from both querys, not a two different WHILE loops, just one WHILE loops with results from both querys?
IS that possible, as example i gave a so simple query to understand :)
As long as the table structure is the same (more specifically, if both queries return the same number of fields and the same datatypes), you can use SQL UNION keyword, for example:
SELECT id, name FROM table1
UNION
SELECT id, name FROM table2
Note though that if the number of returned columns or datatypes do not match, then attempting to execute the query will result in an error, for example,
SELECT id, name, comment FROM table1
UNION
SELECT id, name FROM table2
will not work and will result in an error.
EDIT: with a rewritten question, using OR or IN in the WHERE clause is a much better solution:
SELECT * FROM USLUGE WHERE marka='1' OR marka='2'
or
SELECT * FROM USLUGE WHERE marka IN ('1', '2');
you can use union
SELECT * FROM USLUGE WHERE marka='1'
UNION
SELECT * FROM USLUGE WHERE marka='2'
however you should not use SELECT * type the column names instead
The more data is read from the tables, the slower the query will become. It increases the time it takes for the disk operations. Also when the database server is separate from the web server, you will have longer network delays due to the data having to be transferred between the servers.
It is a good habit to always specify which columns you need when you are doing your SELECT’s.
The question (as edited now) refers to only one table in which case
SELECT * FROM USLUGE WHERE marka='1' OR marka='2'
Would be better than doing a UNION