I have a database as laid out below.
mysql> select * from wants where itemname='lamp' order by location;
+------+--------------------+-------------+----------+----------+----------+
| sess | username | room | image | item | priority |
+------+--------------------+-------------+----------+----------+----------+
| 33 | user1#aol.com | Family Room | DSC00649 | Lamp | 1 |
| 235 | user2#yahoo.com | Family Room | DSC00649 | Lamp | 2 |
| 60 | user3#homtmail.com | Foyer | DSC00527 | Lamp | 1 |
| 197 | user4#gmail.com | Foyer | DSC00527 | Lamp | 2 |
| 189 | user4#gmail.com | Living Room | DSC00827 | Lamp | 1 |
| 273 | user5#live.com | Living Room | DSC00827 | Lamp | 2 |
+------+--------------------+-------------+----------+----------+----------+
6 rows in set (0.00 sec)
What I am trying to do is get a count of each item per room.
When I run the query it should look like:
2 Family Room Lamp user1#aol.com user2#yahoo.com
2 Foyer Lamp user3#homtmail.com user4#gmail.com
2 Living Room Lamp user4#gmail.com user5#live.com
Current Query:
$result1 = mysql_query("SELECT username, location, image, itemname, COUNT( itemname ) x FROM wants GROUP BY itemname HAVING x >1 order by location ASC")
This will give the following output:
6 Family Room Lamp luser1#aol.com user2#yahoo.com user3#homtmail.com user4#gmail.com user4#gmail.com user5#live.com
I tried to use Concat to get the count of the first two rows but that did not help,
How can i break these out to get the rooms to read separately, when the Item name is the same?
You need to group by the room instead of the item. Also sounds like you're looking for group_concat to list the users:
select count(*),
room,
group_concat(username)
from wants
where item ='lamp'
group by room
SQL Fiddle Demo
Related
Ok, I have a single MySQL table with the name 'car' and 3 columns.
+----+--------+------------+
| ID | car_id | engine |
+----+--------+------------+
| 1 | 123 | on |
| 2 | 123 | on |
| 3 | 123 | off |
| 4 | 123 | on |
| 5 | 123 | on |
| 6 | 123 | on |
| 7 | 123 | off |
| 8 | 123 | on |
| 9 | 123 | off |
+----+--------+------------+
Now I want to show the trips this car did. The trips would be determined based on car engine start and stop. For example from the above example we can see that user has made 3 trips as total(From on to off). Now What I want is that if there is a query which gives me only 3 results from on to off meaning if somehow the query groups the records by considering a starting point on and ending point off. Is it possible in mysql? or the other way around is doing it manually by fetching all the records and working in arrays?
At the moment I am fetching all the records and doing it manually by looping all the data and doing accordingly But this process is slow.
Can you try it ?
SELECT * from cars WHERE `engine` = 'off' AND id IN(SELECT id+1 FROM `cars` WHERE `engine` = 'on')
i am trying to build something like an order management tool for a car garage.
The main function of the tool is to manage repair orders and inspection orders as well
Therefore i have created this following three tables.
In the first one you can see the cars and the state of the repair and inspection.
The second one shows the information about the single repair orders the relation between the first and this is 1:n.
The last one shows the automaticly created inspection orders for each car out of the first table. This is an 1:n relation as well.
So what i try to do is to show all the open repair and inspection orders for the cars in one table. But only the open ones.
I tried it with some where statements but i got totaly confused.
My question is, how i can realise it?
+------+--------------------------+----------+------------+
| IDWZ | wz_name | wz_stand | wz_vistand |
+------+--------------------------+----------+------------+
| 1 | Querbr?cke vorn | 0 | 0 |
| 2 | Front Lateral Support | 0 | 1 |
| 3 | Rear Support | 1 | 1 |
| 4 | MID-X-Member Upper Shell | 1 | 1 |
| 5 | Front Lateral Support | 1 | 1 |
+------+--------------------------+----------+------------+
+---------+-----------------+--------------+
| IDWZTBL | rep_wzrepstatus | rep_wzfehler |
+---------+-----------------+--------------+
| 2 | 1 | REP 1 |
| 1 | 1 | REp2 |
| 1 | 1 | REp 3 MASS |
| 1 | 0 | 444 |
| 2 | 0 | |
+---------+-----------------+--------------+
+--------+-------------+
| VIWZID | vi_repstand |
+--------+-------------+
| 1 | 0 |
+--------+-------------+
Sry for that!
So the IDWZ is the foreign KEY in the second table(IDWZTBL) and in the third (VIWZID).
I tried it with
SELECT wz_name, wz_stand, wz_vistand, rep_wzrepstatus, vi_repstand FROM tbl_wz LEFT JOIN tbl_orders ON tbl_wz.IDWZ = tbl_orders.IDWZTBL LEFT JOIN tbl_vi ON tbl_wz.IDWZ = tbl_vi.VIWZID WHERE wz_stand='0' AND rep_wzrepstatus='0' ...
Only for the first table cars to the second one repair orders, that WHERE staement (WHERE wz_stand='0' AND rep_wzrepstatus='0') works fine.
But if i try to add the third table (VI) doing the same, i could fetch the result i wanna have.
What i wanna see in the Overview table is only the last open repair order and the last open inspection order.
i'm new to MySQL and PHP. And i have some problems trying to get data values from two tables in one query using JOIN. What i want to do is query "user_builds" and SUM(amount) where the owner_id=1 AND type=1. The problems comes in now where i have to grab the build_type from another table called "builds".
I have tried to solve this as i mentioned with JOIN, but the closest i came was to get the amount of rows that was equal to how many rows user_id=1 had.
What i want is select the total SUM of "amount"(user_builds) where "type=1"(builds) and "owner_id=1"(user_builds).
I hope you understand what i try to do here, if not i will try to elaborate it more. And also sorry for not providing any of the querys i tried, but as none of them worked it feels irrelevant. Thank you for your time.
Edit:
+-------------------+
| user_builds |
+---------+---------+----------+-------+
| id |owner_id | build_id | amount|
+---------+---------+----------+-------+
| 1 | 1 | 1 | 5 |
| 2 | 2 | 2 | 15 |
| 3 | 2 | 3 | 15 |
| 4 | 1 | 4 | 5 |
| 5 | 1 | 5 | 5 |
| 6 | 1 | 6 | 10 |
+---------+---------+----------+-------+
+----------------------+
| build |
+---------+------------+-----------+--------+
| id | name |description| type |
+---------+------------+-----------+--------+
| 1 | House | desc | 1 |
| 2 | Kitchen | desc | 2 |
+---------+------------+-----------+--------+
I want to query "user_builds" and get the total of "amount" where owner_id=1 and type=1. (type is found in "build" table).
Try this code, I hope it works appropriately.
select sum(ub.amount)
from user_builds ub
left join build b
on ub.build_id = b.id
where b.type=1
and ub.owner_id = 1
select SUM(amount) from user_builds left join builds on build.type = user_builds.type where "owner_id=1"
try this query and replace my query field with your original fields
best of luck...
i have a table in following format:
id | title
---+----------------------------
1 | php jobs, usa
3 | usa, php, jobs
4 | ca, mysql developer
5 | developer
i want to get the most popular keywords in title field, please guide.
If you have a list of keywords, you can do the following:
select kw.keyword, count(*)
from t cross join
keywords kw
on concat(', ', t.title, ',') like concat(', ', kw.keyword, ',')
As others have mentioned, though, you have a non-relational database design. The keywords in the title should be stored in separate rows, rather than as a comma separated list.
If your data is small (a few hundred thousand rows or less), you can put it into Excel, use the text-to-columns function, rearrange the keywords, and create a new, better table in the database.
SELECT title 1, COUNT(*) FROM table GROUP BY title 1
EDIT
Since you've edited and presented a non-normalized table, I would recommend you normalize it.
Have a read of: http://blog.fedecarg.com/2009/02/22/mysql-split-string-function/
You need to modify your database. You should have something like this:
items
+----+---------------+
| id | title |
+----+---------------+
| 1 | something |
| 3 | another thing |
| 4 | yet another |
| 5 | one last one |
+----+---------------+
keywords
+----+-----------------+
| id | keyword |
+----+-----------------+
| 1 | php jobs |
| 2 | usa |
| 3 | php |
| 4 | jobs |
| 5 | ca |
| 6 | mysql developer |
| 7 | developer |
+----+-----------------+
items_to_keywords
+---------+------------+
| item_id | keyword_id |
+---------+------------+
| 1 | 1 |
| 1 | 2 |
| 3 | 2 |
| 3 | 3 |
| 3 | 4 |
| 4 | 5 |
| 4 | 6 |
| 5 | 7 |
+---------+------------+
Do you see the advantage? The ability to make relations is what you should be leveraging here.
I have a closed-source software written in C#/.NET from a VoIP company which is impossible to customize and wanted to create custom front-end using PHP. I gained access to the database and now see how it functions. I wanted to output the user his 'speed dial' numbers, but having issue solving it. Here are the tables structures:
'customer' table
+-------------------------------------------------------------------------+
| CustomerID | FirstName | LastName | Balance | Email | Password | Status |
|-------------------------------------------------------------------------|
| 1 | Homer | Simpson | 5.00 | h#s.s | iheartm | 1 |
| 2 | Marge | Simpson | 3.00 | m#s.s | ihearth | 1 |
+-------------------------------------------------------------------------+
'calls' table
+------------------------------------------------------------------------+
| CallID | Caller | Callee | ServiceID | Duration | Cost | CustomerID |
|------------------------------------------------------------------------|
| 1 | 1234567 | 7654321 | 30 | 60 | 1.00 | 1 |
| 2 | 7654321 | 1234567 | 45 | 120 | 2.00 | 2 |
+------------------------------------------------------------------------+
'ani' (speed-dial) table
+---------------------------------------+
| PhoneNumber | ServiceID | ContactName |
|---------------------------------------|
| 1234567 | 45 | Homer |
| 7654321 | 30 | Marge |
+---------------------------------------+
As you can see, 1234567 is Homer's phone number and in Marge's speed dial list and 7654321 is Marge's number in Homer's list. Just like I can pull up customer's balance when logged in using: $current_user['Balance'];, is there way to show user his 'speed dial' numbers in PHP?
This request doesn't achieve what you want ?
SELECT
a.CustomerID, a.FirstName, a.LastName, a.Balance, a.Email, a.Status,
b.ServiceID,
(SELECT GROUP_CONCAT(CONCAT(ContactName,':',PhoneNumber)) FROM ani GROUP BY PhoneNumber WHERE ServiceID = b.ServiceID)
FROM customer a
LEFT JOIN calls b ON a.CustomerID = b.CustomerID
WHERE a.CustomerID = 'replace_by_customer_id'
This should fetch the data of the customer table, plus a string that results from concatenating the speed-dial numbers of the customer connected.
I assume that a customerid corresponds to one unique serviceid found in calls, and the serviceid in the table ani indicates the owner of the speed-dial number. But it seems a weird architecture, so you should give us more data or informations about the tables...
There seems to be a missing relation from ServiceID to a specific entry in customer. It would seem strange that the calls table would provide that relation, it should merely use it.
With only the information you supplied you can only join calls to link the CustomerID with the ServiceID which I suppose you have. The query would look like this:
SELECT ContactName, PhoneNumber FROM ani
LEFT JOIN calls ON ani.ServiceID=calls.ServiceID
WHERE calls.CustomerID=xxx