This will probably be easy for allot of you guru's.
I am trying to get * from two tables.
First table i can match with a user_id. structure is as follows:
table name: order_new
id | service_id | user_id | total_price | total_price_tax | orderstatus | notes | orderdate
table name 2: order_new_items
id | id_order | name | qty | price
The first table ( order_new ) i can get by using the session id. that first table has a row called "id". That id has items in table 2 ( order_new_items ) under the row of "id_order".
Im not sure on how to join the two and pull all data from both tables matching id from first table and id_order from second table
SELECT * FROM order_new, order_new_items where order_new.id = order_new_items.id and order_new.id = 4711
This will retrieve all rows where an ID exists in bot tables. It will not retrieve rows from table order_new when there are no corresponding rows in order_new_items (i.e. empty order)
To achieve this, you need to use:
SELECT * FROM order_new
LEFT JOIN order_new_items on order_new.id = order_new_items.id
where order_new.id = 4711
probably you need to list columns explicitly instead of *
MySQL-Only:
SELECT * FROM order_new o, order_new_items i WHERE user_id = '7' AND o.id_order = i.id;
I'm assuming, that id from order_new is the primary key for the table, while id_order is the foreign key for the 1:n relationship.
To be noted, the 7 is an example of course and needs to be substituted with whatever value you're looking for.
According to comments, I'm answering another question:
$result = mysql_query("SELECT * FROM order_new WHERE user_id = 7");
while ($row = mysql_fetch_array($result)) {
//store order information
$res2 = mysql_query("SELECT * FROM order_new_items WHERE id_order = $row[id]");
while($row2 = mysql_fetch_array($res2)) {
//store further information
}
}
Try this, this will fetch you data from both tables based on conditon.
SELECT a*, b.id, b.name, b.qty, b.price FROM order_new a
INNER JOIN order_new_items b on b.id_order = a.id
WHERE a.id = 100
Related
I know this is similar to this question but there is still no working answer yet and I will try to explain better.
So I have THREE tables, which are member, meta_name, and meta_value. I think you already know how they are related to each other. For example, assume I have these rows:
member table:
memberID | name
1 | john
meta_name table:
meta_nameID | name
1 | address
2 | jobTitle
meta_value table:
meta_valueID | meta_nameID | memberID | value
1 | 1 | 1 | California
2 | 2 | 1 | Manager
So John has two meta data which are address and jobTitle. The meta data are stored in meta_value table, and the mete_value table has identifiers in meta_name table. It's just a basic meta data system.
Now the question is, how can I get members that fulfill two or more conditions on the meta_value table? Something like, "get members that have an address at California AND a jobTitle as Manager"?
I have tried this query:
SELECT * FROM member JOIN meta_value ON member.memberID = meta_value.memberID WHERE (meta_nameID = '1' AND value = '3') AND (meta_nameID = '2' AND value = 'Jonggol')
I know that's an ugly-not working query but I hope that will help you understand what I'm going to achieve. Thanks!
NOTE: I actually don't need the meta_value table data. I just want to get members that fulfill the conditions.
There are a number of different options.
Straightforward:
SELECT
*
FROM
member
WHERE
EXISTS (
SELECT
*
FROM
meta_value
WHERE
meta_value.memberID = member.memberID
AND meta_value.meta_nameID = 1
AND meta_value.value = '3'
)
AND EXISTS (
SELECT
*
FROM
meta_value
WHERE
meta_value.memberID = member.memberID
AND meta_value.meta_nameID = 2
AND meta_value.value = 'Jonggol'
)
SELECT
*
FROM
member
WHERE
memberID IN (
SELECT
memberID
FROM
meta_value
WHERE
meta_value.meta_nameID = 1
AND meta_value.value = '3'
)
AND memberID IN (
SELECT
memberID
FROM
meta_value
WHERE
meta_value.meta_nameID = 2
AND meta_value.value = 'Jonggol'
)
Another way:
SELECT
member.*
, SUM(IF((meta_value.meta_nameID = 1 AND meta_value.value = '3') OR (meta_value.meta_nameID = 2 AND meta_value.value = 'Jonggol'), 1, 0)) AS x
FROM
member
INNER JOIN meta_value ON (
meta_value.memberID = member.memberID
)
GROUP BY
member.memberID
HAVING
x = 2
However, I'd like to note that such DB schema should only be used to store data that require filtering.
You need to use sub queries. Try the following query to select members with a particular address and a job title.
SELECT member.name
FROM member
WHERE
memberID IN
(SELECT DISTINCT memberID
FROM meta_value
WHERE meta_nameID IN
(SELECT DISTINCT meta_nameID FROM meta_name WHERE name='address')
AND value='California')
AND memberID IN
(SELECT DISTINCT memberID
FROM meta_value
WHERE meta_nameID IN
(SELECT DISTINCT meta_nameID FROM meta_name WHERE name='jobTitle')
AND value='Manager')
You also try a smaller query using with clause:
(Here we are creating a tmp table which has both address and job title, later join the address separately to get just address and jobTitle to get just job title. This will give you a member level table with address and jobTitle as columns for easy use in any subsequent queries)
WITH tmp AS
(SELECT * FROM meta_value mv INNER JOIN meta_name mn ON mv.meta_nameID=mn.meta_nameID)
SELECT member.name , add.value , job.value
FROM member
LEFT JOIN (SELECT * FROM tmp WHERE name='address') add ON member.memberID = add.memberID
LEFT JOIN (SELECT * FROM tmp WHERE name='jobTitle') job ON member.memberID = job.memberID
WHERE add.value = 'required address' AND add.job.value ='required job title'
I need help with a sql query.
I have these 2 tables:
player_locations:
ID | playerid | location <- unqiue key
---|-----------------------
1 | 1 | DOWNTOWN
and users:
ID | playername | [..]
----|--------------------
1 | example1 | ...
I need a select to get the users.playername from the player_locations.playerid. I have the unique location to get the player_locations.playerid.
Pseudo query:
SELECT playername
FROM users
WHERE id = player_locations.playerid
AND player_locations.location = "DOWNTOWN";
The output should be example1.
This is just a simple INNER JOIN. The general syntax for a JOIN is:
SELECT stuff
FROM table1
JOIN table2 ON table1.relatedColumn = table2.relatedColumn
In your case, you can relate the two tables using the id column from users and playerid column from player_locations. You can also include your 'DOWNTOWN' requirement in the JOIN statement. Try this:
SELECT u.playername
FROM users u
JOIN player_locations pl ON pl.playerid = u.id AND pl.location = 'DOWNTOWN';
EDIT
While I personally prefer the above syntax, I would like you to be aware of another way to write this which is similar to what you have now.
You can also select from multiple tables by using a comma in your FROM clause to separate them. Then, in your WHERE clause you can insert your conditions:
SELECT u.playername
FROM users u, player_locations pl
WHERE u.id = pl.playerid AND pl.location = 'DOWNTOWN';
Here is the solution.
SELECT
playername
FROM users
WHERE id = (SELECT id FROM player_locations WHERE location='DOWNTOWN')
I have a idea, try this:
SELECT playername
FROM users
WHERE id IN (SELECT DISTINCT playerid FROM player_location WHERE location LIKE "DOWNTOWN");
I am trying to join two tables with similar ids, then get a sum of two fields as well. let me explain:
test table: id | post | desc | Date
likes_dislikes table: id | song_id | user_ip | like | dislike
on test 'test table', the 'id' matches that of the likes_dislikes 'song_id', so I tried LEFT JOIN since not every post will have an id in the likes_dislikes table, but I got duplicate results .
SELECT *
FROM
test
LEFT JOIN likes_dislikes ON test.song_id = likes_dislikes.page_id
GROUP BY test.song_id
ORDER BY test.id DESC LIMIT $start, $limit
how can I prevent the duplicate content, and also, get the TOTAL likes/dislikes associated with each post as I run through a while loop?
I Assume you are looking for something like this:
SELECT
T.`id`,
T.`post`,
T.`desc`,
T.`Date`,
COUNT(L.`like`) as `LikeCount`,
COUNT(L.`dislike`) as `DislikeCount`
FROM `test` T
LEFT JOIN `likes_dislikes` L
ON T.`Id` = L.`song_id`
GROUP BY T.`Id`, T.`post`, T.`desc`, T.`Date`
ORDER BY T.`id` DESC;
SELECT deals.id,
deals.partner_id
FROM deals
LEFT JOIN deals_partners
ON ( deals.id = deals_partners.deal_id )
WHERE 1
AND ( `deals`.`partner_id` = 222
OR CASE
WHEN deals.partner_count = 1 THEN
deals_partners.partner_id = 222
end )
ORDER BY deals.id ASC
I would like to grab the deals that are associated to the partner.
They can either be master of the deal, deals.partner_id = 222 or they can have a row in deals_partners where they get linked to the deal.
The above works out for me, but gives me multiple of the same deals, because of the count of deals_partners I have. I made it a left join, I dont understand why it still keeps grabbing rows from the deals_partners?
Update:
table: deals, columns: ID, title, name, partner_id
table deals_partners, columns: deal_id, partner_id
I would like to display the partner 222's deals. To find out which deals, that the partner has, his partner_id can either be in the deal row, in the column partner_id or he can have a row in deals_partners where his partner_id is linked to the deal_id.
When you join to a table and match on multiple records, you will return multiple records. You can use the IN clause to prevent this.
SELECT deals.id,
deals.partner_id
FROM deals
WHERE `deals`.`partner_id` = 22
OR id in
(SELECT deal_id
FROM deals_partners
WHERE partner_id = 222)
ORDER BY deals.id ASC
Try something like: (hope it works)
SELECT deals.id,
deals.partner_id
FROM deals
LEFT JOIN deals_partners
ON deals.id = deals_partners.deal_id AND
deals_partners.partner_id = 222
WHERE `deals`.`partner_id` = 222 OR deals_partners.partner_id = 222
ORDER BY deals.id ASC
This assumes there is only one match for deals.id = deals_partners.deal_id AND deals_partners.partner_id = 222. Otherwise you'll need to add GROUP BY deals.id, deals.partner_id, deals_partners.partner_id.
I have a two simple tables
users
+----+--------+-----------+
| id | gender | birthdate |
+----+--------+-----------+
userpreference
+----+------------------+-----------------+
| id | preference value | preference type |
+----+------------------+-----------------+
Question:
I want to query all people who have not listed a specific preference value such as 'shopping'.This includes all people who have not listed any preference types as well so that column could be null, however since userpreference's column 'id' references users as a foreign key, I also want to include in my count all people who don't show up in the second table (user preference)?
Total # of people who do not have preference value 'shopping' as their preference value:
Here is what i have tried:
SELECT
(
SELECT COUNT(DISTINCT userpreference.id) FROM userpreference
WHERE preferencevalue != 'shopping')
+
(
SELECT COUNT(users.id)
FROM users
WHERE users.id NOT IN
(SELECT userpreference.Id
FROM userpreference )
)
AS'Total'
Select Count(*)
From Users
Where Not Exists (
Select 1
From UserPreference As UP1
Where UP1.id = Users.id
And UP1.PreferenceValue = 'Shopping'
)
Try a RIGHT JOIN, that will include all people who dont show up in the second table
SELECT *
FROM Users
RIGHT JOIN Userpreference ON ( users.userID = Users.userID)
WHERE preference_value = 'shopping'
Try this:
SELECT COUNT(DISTINT U.id) FROM users U NATURAL LEFT JOIN userpreference UP
WHERE UP.preferencevalue IS NULL OR UP.preferenceValue != 'shopping';
The LEFT JOIN should bring in all the users records whether or not they have a UP record.