MySQL select one field from table WHERE condition is in multiple rows - mysql

Tried to find the answer, but still couldn't.. The table is as follows:
id, keyword, value
1 display 15.6
1 harddrive 320
1 ram 3
So what i need is something like this.. Select an id from this table where (keyword="display" and value="15.6") AND (keyword="harddrive" and value="320")
There's also a possibility that there will be 3 or 4 such keyword conditions which should result into returning one id (one row)
It seems there's something to deal with UNION but i didn't use it before so i can't figure it out
Thanks in advance

This is a relational division problem. Something like the following should do it.
SELECT id
FROM your_table
WHERE
(keyword="display" and value="15.6") OR (keyword="harddrive" and value="320")
GROUP BY id
HAVING COUNT(*) = 2
I'm assuming that your table has appropriate constraints such that it is impossible for there to be a completely duplicated row. (e.g. there is a PK on id, keyword)

SELECT DISTINCT id FROM table
WHERE
(keyword="display" and value=15.6) OR (keyword="harddrive" and value=320)

Related

mysql select statement IN

I am having two table
Table 1 having a field
id
book_ids
1
1,2,3
Table 2 have all the book Ids
select *
from table 2
where book_id in (select book_ids from table 1 where id=1) ;
this statement not returning all the book ids from table 2 having id 1,2,3
Can anyone help
You could use the FIND_IN_SET() function:
select *
from table 2
where FIND_IN_SET(book_id, (select book_ids from table 1 where id=1)) > 0;
Read the documentation I linked to for details on how that function works.
But only do this if your table remains small. Using this function spoils any opportunity to optimize the query with an index, so the larger your table gets, the performance will grow worse and worse.
Also FIND_IN_SET() doesn't work the way you expect if there are spaces in your comma-separated list.
try to store table1 values in rows of table not in a same field.
and then your SELECT IN works.

MySQL SELECT Statement query join 2 tables

I have a problem when I write query select statement I have a lot of row duplicated. My table like this:
tbl_injection
tbl_patient
My Expect result is wanted to return a row because my table tbl_patient has only 1 row. And I just want to get the injection_status from tbl_injection.
Seems like patient_id is not unique enough. Try to put more filter such as injection_date maybe?
Or you can return the result of "latest" injection_status instead.
As you see, patient_id value 1 of tbl_patient is recorded 16 times in tbl_injection table, So you can not one low by using patient_id column only.
If you don't want to change those table records, you have to make a query additional columns, such as injection_data which has unique value.

it is possible to "group by" without losing the original rows?

i have a query like this:
ID | name | commentsCount
1 | mysql for dummies | 33
2 | mysql beginners guide | 22
SELECT
...,
commentsCount // will return 33 for first row, 22 for second one
FROM
mycontents
WHERE
name LIKE "%mysql%"
also i want to know the total of comments, of all rows:
SELECT
...,
SUM(commentsCount) AS commentsCountAggregate // should return 55
FROM
mycontents
WHERE
name LIKE "%mysql%"
but this one obviously returns a single row with the total.
now i want to merge these two queries in one single only,
because my actual query is very heavy to execute (it uses boolean full text search, substring offset search, and sadly lot more), then i don't want to execute it twice
is there a way to get the total of comments without making the SELECT twice?
!! custom functions are welcome !!
also variable usage is welcome, i never used them...
You can cache the intermediate result to a temporary table, and then do the sum over this table
One obvious solution is storing intermediate results withing another 'temporary' table, and than perform aggregation in the second step.
Another solution is preparing a lookup table containing sums you need (but there obviously needs to be some grouping ID, I call it MASTER_ID), like that:
CREATE TABLE comm_lkp AS
SELECT MASTER_ID, SUM(commentsCount) as cnt
FROM mycontents
GROUP BY MASTER_ID
Also create an index on that table on column MASTER_ID. Later, you can modify your query like that:
SELECT
...,
commentsCount,
cnt as commentsSum
FROM
mycontents as a
JOIN comm_lkp as b ON (a.MASTER_ID=b.MASTER_ID)
WHERE
name LIKE "%mysql%"
It also shouldn't touch your performance as long as lookup table will be relatively small.
A GROUP BY on one of the ID fields might do the trick. This will then give you the SUM(commentsCount) for each ID.
The query in your question is not detailed enough to know which of your fields/tables the ID field should come form.

MySQL 5.5 SELECT DISTINCT - How to return more than one column with no query duplicate?

Seems like on MySQL 5.5 SELECT DISTINCT works fine just with one column.
SELECT DISTINCT type FROM table
WHERE type LIKE 'h%'
LIMIT 5;
returns good result:
type
--------
htm
html
htaccess
But on trying SELECT two or more columns
SELECT DISTINCT id, type FROM table
WHERE type LIKE 'h%'
LIMIT 5;
it returns failed result with duplicated queries:
id | type
---+--------
1 | htm
3 | htm
5 | html
6 | html
7 | html
Expected result:
id | type
---+--------
3 | htm
7 | html
5 | htaccess
Column id has no need to apply DISTINCT, since it has AUTO_INCREMENT.
You want a group by:
select
max(id) as id,
type
from
table
where type like 'h%'
group by type
distinct gets you distinct rows--that means that unless every value in every column is the same as a row already in the result set, it'll show that row. In this case, it's behaving exactly as described.
By doing a group by and using aggregate functions (like max), you're specifying the columns that you want to be distinct, while you aggregate the other columns up to provide you the result set that you're looking for.
SELECT
min(id) as id,
type
FROM table
WHERE type LIKE 'h%'
GROUP BY type
LIMIT 5;
They are not duplicated rows. Distinct means that each combination of column values is different for all others. As you can see, 1|htm and 3|htm is not the same, then is different.
First, for clarification:
SELECT DISTINCT id, type FROM table
WHERE type LIKE 'h%'
LIMIT 5;
This query works as expected, because id is an auto_increment, there are no dupes, so distinct is pointless.
Second, what are you trying to accomplish? Whether you do a DISTINCT or GROUP BY on the type column, you are going to get an ID which only represents one row in the table, even though several rows in the table share the same value (each with a different id).
MySQL 5.5
SELECT DISTINCT - How to return one column with no query duplicate?
If you have an employee table like:
Eno Ename Location
When you have more than one location for an employee, and you want to see unique locations:
SELECT DISTINCT
Eno,Ename,(Location)
FROM
Employee

mySQL select columns from different tables

1000 Apologies if I've repeated a question, couldn't find an answer here to my question.
I'm try to retrieve the data from 2 separate columns from 2 unrelated tables in the same query.
I've tried using a UNION statement, but the problem is that I need to be able to separate the results into 'venues' and 'programmes' - here was what I did:
SELECT venue_name
FROM my_venues
UNION
SELECT programme_title
FROM my_programmes;
Maybe it's not necessary to combine the query and I can just do 2 separate queries? The database won't be especially large, but it seems unnecessary...
Help and thanks!
Just add a constant column in both selects, with the same name, but different values:
SELECT "venues" as source, venue_name as thing_name
FROM my_venues
UNION ALL
SELECT "programmes" as source, programme_title as thing_name
FROM my_programmes;
Now:
Rows with value "venues" for column
source will come from the table
my_venues ,
rows with value "programmes" for
column source will come from table
my_programmes.