Advertisement

Author Topic: php mysql query  (Read 2739 times)

0 Members and 1 Guest are viewing this topic.

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
php mysql query
« on: September 28, 2010, 11:24:05 PM »
I ve got a table like this and need the query to get the result.  anyone?

MY BAD, THE IDS DO HAVE DUPES...UPDATE D.


ID   Name   Type   Value

1   Store1   Region   9000   
2   Store2   Region     9000
3   Store3      Region     8000

1   Store1   Service   6000
2   Store2   Service     6000
3   Store3   Service     6000

1   Store1   Wage   5555
2   Store2   Wage   4444
3   Store3   Wage   3333


I want to get all Name where Region = 9000 and Service = 6000

The result should be Name is Store1 and Store2.

What is the query to get this?

select store where region = 9000 and service = 6000 doesn't pull it?


« Last Edit: September 29, 2010, 02:10:44 PM by darkhelmet »

Like this post: 0
I made you read this.

Adverstisement

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php mysql query
« Reply #1 on: September 29, 2010, 12:22:34 AM »
i know but but it's not my table design, i just have to get data from it. :(



Like this post: 0
I made you read this.

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php mysql query
« Reply #2 on: September 29, 2010, 08:38:26 AM »
i had another developer look at it too and he was also stumped as well.  he didn't have any idea off hand.



Like this post: 0
I made you read this.

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php mysql query
« Reply #3 on: September 29, 2010, 08:45:42 AM »
I think i know where you're going with this.  It might just work with some tweaks.  I'm going to do some test right now.


Wow, then it's going to be difficult. I'm not even sure if you can do it in one query.
Because you'll have to test for the Type= Region AND Value=9000, but then you can't test for Type=Service again...
unless you did more than one query. Plus that entails that you do filtering in your php code.

If doing more than one query isn't an issue than I would suggest:

SELECT Name FROM table WHERE Type='Region' AND Value='9000'

Then take the result and use it in a new query:

SELECT * FROM table WHERE Name='result' AND Type='Service' AND Value='6000'

That's all I can think of without actually writing the code and testing it. Hope this helps.



Like this post: 0
I made you read this.

jbutton

  • Guest
Re: php mysql query
« Reply #4 on: September 29, 2010, 09:03:50 AM »
Try this:
Code: [Select]
select Name where (Type='Service' and Value = 600) or (Type = 'Region' and Value = 900)



Like this post: 0

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php mysql query
« Reply #5 on: September 29, 2010, 09:51:49 AM »
I got a seasoned developer to answer my question.

It seems like you can do it in one query.  Something like this

SELECT a.name, count( id ) as value_count FROM table AS a WHERE  ( (type = region AND value IN (9000)) OR (type = service AND  value IN (6000)) ) GROUP BY a.id HAVING value_count = 2



Like this post: 0
I made you read this.

jbutton

  • Guest
Re: php mysql query
« Reply #6 on: September 29, 2010, 10:37:31 AM »
I got a seasoned developer to answer my question.

It seems like you can do it in one query.  Something like this

SELECT a.name, count( id ) as value_count FROM table AS a WHERE  ( (type = region AND value IN (9000)) OR (type = service AND  value IN (6000)) ) GROUP BY a.id HAVING value_count = 2


Did you try my code?  Did it work?



Like this post: 0

jetter

  • Guest
Re: php mysql query
« Reply #7 on: September 29, 2010, 11:03:50 AM »
Or you can get it simply using:

Code: [Select]
SELECT Name FROM <TABLE> WHERE (Type='Region' AND  Value = '9000') OR (Type='Service' AND Value= '6000') GROUP BY Name
Assuming the two types "region" and "Service" are different and you want the ability to say, if a Name has A or B then the above should work.  If you change the A or B to AND then you simply change the OR to AND.

Jbutton got the right idea but it will get duplicates, op just wants the name and no duplicates so adding the GROUP BY clause will accomplish that.

OP original assumption is an error because it seems "region = 9000 and service = 6000"
region, service are text within the type field and 9000, 6000 are values within the value field.


« Last Edit: September 29, 2010, 11:08:27 AM by jetter »

Like this post: 0

jetter

  • Guest
Re: php mysql query
« Reply #8 on: September 29, 2010, 11:07:33 AM »
I got a seasoned developer to answer my question.

It seems like you can do it in one query.  Something like this

SELECT a.name, count( id ) as value_count FROM table AS a WHERE  ( (type = region AND value IN (9000)) OR (type = service AND  value IN (6000)) ) GROUP BY a.id HAVING value_count = 2

this will return duplicates since you are grouping by ID and id are different for each entry but has the same name.  This query is way to slow because it uses too many built in SQL functions that you do not need.  What is the purpose of counting id?  You are not using it and by Grouping by ID?  what is that used for since the id are unique anyway; so this makes the having clause pointless because the id will all have 1 as their count and the sql will error out because group by clause requires every field in the returning tuples to be group also.


« Last Edit: September 29, 2010, 11:09:51 AM by jetter »

Like this post: 0

jbutton

  • Guest
Re: php mysql query
« Reply #9 on: September 29, 2010, 11:28:13 AM »
From the table, I conclude that there can be no duplicates.

Why would you have duplicates?  Makes no sense.

OK, I forgot.. I was thinking about row and not Name.  Names duplicates.



Like this post: 0

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php mysql query
« Reply #10 on: September 29, 2010, 02:00:01 PM »
jbutton, thanks for the input.  i originally had used the same query as you suggested but gave too many results.

Did you try my code?  Did it work?



Like this post: 0
I made you read this.

Offline darkhelmet

  • Moderator
  • *****
  • Posts: 1557
  • Respect: +56
    • View Profile
Re: php mysql query
« Reply #11 on: September 29, 2010, 02:08:34 PM »
thanks jetter, you have the right answer and i understand what you're saying.  my bad, my table was wrong.  the field `id` do have dupes so grouping by id is appropriate.  sorry, just got sloppy in my post.  

the counting is required...the table i posted above is just a very very shortened version of the one i'm working on.
the count is for couting the number of 'type' because i need to be able to search by any number of type, it counts the number of matches and the total count must match the number of types i'm searching for.

thanks for your input.





this will return duplicates since you are grouping by ID and id are different for each entry but has the same name.  This query is way to slow because it uses too many built in SQL functions that you do not need.  What is the purpose of counting id?  You are not using it and by Grouping by ID?  what is that used for since the id are unique anyway; so this makes the having clause pointless because the id will all have 1 as their count and the sql will error out because group by clause requires every field in the returning tuples to be group also.


« Last Edit: September 29, 2010, 02:13:39 PM by darkhelmet »

Like this post: 0
I made you read this.

jetter

  • Guest
Re: php mysql query
« Reply #12 on: September 29, 2010, 03:21:00 PM »
grouping by id is not worth it because it will add more overhead to it,  why not just count the name instead.  You will have the same count of the names; which is the same as the id, and since you already are returning it, it makes the query more efficient.




Like this post: 0

 

Advertisements