I'm trying to get this output with MySQL:
-Kate
-William
-dog
(3 results)
From those databases:
--------------- ----------------------
| users | | pet |
|-------------| |------|------|-------
| id | Name | | user|animal|number|
|-------------| |------|------|------|
| 1 |Kate | | 1 | cat | 0 |
| 2 |William| | 2 | dog | 1 |
--------------- ----------------------
Number needs to be != 0
and I need to be able to make the difference between a fetch where number = 0 and number != 0
SELECT
name, animal
FROM
users
INNER JOIN pet ON users.id = pet.user
WHERE
number != 0'
I can't get 'Kate' because never matching != 0.
I think I should use 2 different WHERE conditions in one request, but I don't know how...
First give your tables an alias because you have a ambigious column name id.
SELECT u.name, p.animal
FROM users AS u
INNER JOIN pet AS p
ON u.id = p.id
WHERE p.number != 0
But what you asking for is to get results from both tables without join, right? Like this
SELECT
name AS creatur,
'users' AS type
FROM users
UNION
SELECT
animal AS creatur,
'pet' AS type
FROM pet
WHERE number != 0
First. Use aliases for tables. Use, for example, 'u' for users table and 'p' for 'pet'. Mysql do not understand which field get from which table without aliases.
Second. Condition in WHERE section not related to JOIN. Use AND in ON section.
Third. Use LEFT JOIN instead of INNER JOIN and read articles about types of JOIN and differency between them.
Fourth. In such cases in users table usually adding field pet_id. Do not use single id field for both entities. This thing named ONE-TO-MANY relation. If you want use MANY-TO-MANY relation, you must add third table with two fields user_id and pet_id.
But this query may solve your question:
SELECT u.Name, p.animal
FROM users AS u
LEFT JOIN pet AS p ON u.id = p.user AND p.number != 0
Related
I want to work on SQL. inner join is not working missing. I am using two tables get data on "city_id " field these table code so please help me
inventory
==========================================
inventory_id | city_id | title |is_enabled
==========================================
1 | 1 | abc | 1
2 | 1 | bcd | 1
cities
====================================
city_id | city | title
===================================
1 | delhi | abc
2 | nodia | bcd
SELECT * FROM inventory i
INNER JOIN cities c
ON i.city_id = c.city_id
WHERE i.is_enabled = 1
ORDER BY i.inventory_id DESC LIMIT 10;
Some databases (such as MS Access and MySQL) do not support FULL OUTER JOIN.
However, FULL OUTER JOIN is rarely needed -- and almost never when you are using keys defined between tables. Well structured databases have keys with appropriate values.
I suspect an INNER JOIN does what you want:
SELECT . . . -- list out the columns you want
FROM inventory i INNER JOIN
cities c
ON i.city_id = c.city_id
WHERE i.is_enabled = 1
ORDER BY i.inventory_id DESC
LIMIT 10;
This query assumes that the inventory rows have a valid city_id. If some are NULL (an allowed, non-matching value), you can use LEFT JOIN instead.
Some other notes:
List out the columns you want explicitly. In particular, duplicate column names can be problematic.
Use table aliases, so the query is easier to write and to read.
Qualify all column names. I'm guessing that is_enabled comes from inventory. I should not have to guess.
Do not put single quotes around numeric constants. I am guessing that is_enabled is a number, not a string.
So I have the following tables
**accounts**
id | iban
1 | ES80 2310 0001 1800 0001 2345
2 | ES91 2100 0418 4502 0005 1332
**acc_rel**
account_id | target_table | target_id
1 | users | 2
2 | clients | 5
**users**
id | username | password
2 | abc | cba
**clients**
id | company_name
5 | some_name
So the thing is that with acc_rel I have an account related with an user, a client, or whatever other table.
Because of reasons, not my reasons, I can't change this tables or the way they "works".
What I need to do is, retrieve the account information with the username or the company_name from the other tables, I need to do it in a single query so I can use a WHERE to filter, or ORDER BY and LIMIT. At least I think that I need it in a single query to do so.
This would be the perfect output:
account_id | account_owner
1 | abc
2 | some_name
So how can I do it? I don't know which tables I am going to do the JOIN or what column name I need, thought I can know it with PHP before doing the query.
A solution changing all the tables scheme would be appreciated too if the correct way to do this is another.
Based on #knowledge... answer I made this query and it works exactly like I need it.
SELECT AR.account_id,COALESCE(U.username, C.company_name) AS account_owner
FROM accounts AS A
INNER JOIN acc_rel AS AR ON A.id = AR.account_id
LEFT JOIN users AS U ON U.id = AR.target_id AND AR.target_table = 'users'
LEFT JOIN clients AS C ON C.id = AR.target_id AND AR.target_table = 'clients'
It is going to be a pain to maintain if I need to add new tables but well, it works the way I need.
select AR.account_id,COALESCE(U.username,C.company_name) as account_owner
from accounts A
join acc_rel AR on A.id=AR.account_id
left join users U on U.id=AR.traget_id and AR.target_table='user'
left join clients C on C.id=AR.traget_id and AR.target_table='clients'
A user can have two types of relationships on my website. Following or Friends. I want to populate each user's news feed with the posts from users they have a relationship with. So far I have this join:
SELECT *
FROM posts a
LEFT JOIN relationships b
ON a.user_id = b.user_2
WHERE b.user_1 = $user_id AND
b.status IN (1,3,4) OR a.user_id = $user_id
ORDER BY a.post_id DESC
b.status is the column from my relationships table that determines the status of the relationship between two users. 1 being following, 3 being friends, 4 being following with a pending friend request. My join works well, except that It it doesn't take into consideration that there is only one row in my 'relationships' table that can represent a friend ship between any two people. There are two rows for following. The first being user 1(mark) following user 2(matt) and user 2(matt) following user 1(mark). But with friendships it is either a row stating one way or the other, with a status of 3. First off, is that smart practice? Or should I have two rows for this type pf relationship as well? Secondly, how can I make my JOIN QUERY also grab posts from users with where there is a relationship between user_1 and user_2 and the status = 3? With no regard to the order user_1 and user_2 are in?
Table Structure:
Posts:
| user_id | post_id | story |
-----------------------------
| 1 | 1 | text. |
-----------------------------
Relationships:
| rel_id | user_1 | user_2| status |
--------------------------------------
| 1 | 1 | 2 | 3 |
--------------------------------------
In your WHERE statement, do something like this so you match either column in the relationship table. The OR gets both of them.
WHERE b.user_1 = $user_id OR b.user_2 = $user_id
Good luck!
I have two tables, one table is called queuelist and the other is call info. In the queuelist table it just lists different IDs. I am trying to get the 'clientID' from that table and match it with the 'ID' in the other table that contains all of the info and display it back on the page. Here is how the tables look:
Table - queuelist
ID | clientID
-------------
1 | 589
2 | 254
3 | 486
Table - info
ID | Name | Phone
--------------------
256 | Bob | 5551231234
486 | Jack | 5551231234
589 | Jill | 5551231234
This is what they call joining tables, you should use a query like this:
SELECT i.ID, i.Name, i.Phone FROM `queuelist` AS q
LEFT JOIN `info` AS i ON (
q.clientID = i.ID
);
I'm using aliases for shorter notation in the above query (queuelist becomes q and info becomes i) and then set the join condition (the bit between the ON()) to be the clientID from the queuelist table should match the ID in the info table.
Also see http://dev.mysql.com/doc/refman/5.0/en/join.html for more details.
You need to use an inner join
select * from queuelist as ql inner join info as i on ql.clientID = i.ID
Though you might want to replace * with specific field names e.g
select ql.clientID, i.fieldname FROM....
Well, I see no difficulty in this using a JOIN.
SELECT * FROM queuelist JOIN info ON clientID = info.ID WHERE queuelist.ID = 2
"Where" would be another option.
SELECT Name, Phone FROM queuelist,info WHERE clientID = ID
Assuming you want only name and phone
I have many a times tried using nested query for MySQL in PHP, but it does not work. Is it not possible to do nested/Joins queries?
Just a Scenario:
I have two tables one table with user id and the other with data. User logins and with sessions I have to cross check two different tables with user id (user and data). Is it not possible to nest/join these two tables to write a single query statement.
In short is nesting or joining two or more tables permitted in PHP coding?
YES, it is possible to join two or more tables in MySQL (and therefore, also when using PHP).
You need to post your table schema, if you want us to show a relevant join query. You could, however, try something like:
SELECT * FROM user AS t1
CROSS JOIN data AS t2
ON t1.userid=t2.userid
WHERE t1.userid='154'
(This query presumes that there always will be one row with the userid in both tables. You should use LEFT JOIN instead of CROSS JOIN to return a row even if there is no row in data for the userid. 154 is just an example userid.)
Have a look at http://dev.mysql.com/doc/refman/5.5/en/join.html for information on the JOIN syntax.
users
| user_id | username | password | enabled |
|---------|----------|----------|---------|
| 1 | john | sgsd2gg | 1 |
| 2 | jane | sdshdhd | 0 |
users_data
|udata_id| user_id | some_column |
|--------|---------|-------------------|
| 1 | 1 | Some title |
| 2 | 2 | another title |
Since you haven't posted your table schema, I can't give you an exact solution. But supposing you have a users table and a users_data table, where users_data are owned by a user. You can do a join on the table to retrieve all the data.
SELECT * -- Don't select all fields unless you need it
FROM users U LEFT JOIN users_data UD ON U.user_id = UD.user_id
WHERE U.user_id = 1
This would pull all the records for user with an ID of 1. This is a very simplistic join, but it should give you an idea.
Here's an example that visually describes the different options you can use : SQL Join Differences