Hello! I need help i have table that has this value
Table1
sorter | valuedata | status | useraccounts |
----------------------------------------------
same | value1 | Disabled | user1 |
same | value2 | Active | user1 |
And
Table2
name | useraccounts |
-------------------------
user1 | displayname1 |
and I wanted to display all the data even they have different status while they are innerjoin like this
SELECT table1.name,table2.useraccounts
FROM table1 INNER JOIN table2 ON table1.useraccounts = table2.name
WHERE table1.sorter = "same" AND table1.status = "Disabled" AND table1.status != "Disabled"
is this even possible to do??
I think you made 2 mistakes:
table 1 does not have name column
what you are look for is an OR instead of AND.
The next is what I think you are looking for:
SELECT table2.[name],table2.useraccounts
FROM table1 INNER JOIN table2 ON table1.useraccounts = table2.name
WHERE table1.sorter = 'same' AND
(table1.status = 'Disabled' OR table1.status != 'Disabled')
and this can be rewritten to the next since checking for 'Disabled' or != 'Disabled' is ALL ROWS.
SELECT table2.[name],table2.useraccounts
FROM table1 INNER JOIN table2 ON table1.useraccounts = table2.name
WHERE table1.sorter = 'same'
Actually, you need the cross join. You can write it without using the JOIN as well:
SELECT table2.name,table2.useraccounts
FROM table1,table2 WHERE table1.useraccounts = table2.name
AND table1.sorter = "same"
SELECT table2.name,table1.useraccounts
FROM table1 INNER JOIN table2 ON table1.useraccounts = table2.name
U will get all data while status is differnt or not
Related
I have the table news_feed in which all of my different types of activities data will be stored like admin activities, user activities, company activities etc. The table format looks like:
news_id | type | admin_id | user_id | company_id
1 | admin | 2 | 3 | 0
2 | user | 3 | 4 | 1
3 | company | 0 | 1 | 2
Suppose a user with an id 1 has liked the company which has id 2 then the record will be inserted like
4 user 0 1 2
And I'm listing them in my module and the listing is perfect. But suppose if the company id 2 doesn't exist or if it is inactive, then the news_feed block in listing getting empty. What I want to do is:
If the type is company then JOIN the company table while select listing with condition for status as active
If the type is user then JOIN the user table while select listing with condition for status as active
Well you can use UNION for this issue
SELECT t.column1, t.column2, t.column3
FROM my_table t INNER JOIN company_table c
ON t.company_id = c.id
WHERE c.active=1 AND t.type = "company"
UNION
SELECT column1, column2, column3
FROM my_table t INNER JOIN user_table u
ON t.user_id = u.id
WHERE c.active=1 AND t.type = "user"
Just to add, to increase the efiiciency use UNION ALL rather than UNION (or UNION DISTINCT) as UNION requires internal temporary table with index (to skip duplicate rows) while UNION ALL will create table without such index, but keep in mind it will skip the repeated data.
But more optimized way to do a Conditional Join in MySQL by using a INNER JOIN
SELECT t.column1, t.column2, t.column3
FROM my_table t
INNER JOIN company_table c
ON (t.company_id = c.id AND t.type = "company" AND c.active=1)
INNER JOIN user_table u
ON (t.user_id = u.id AND t.type = "user" AND u.active=1);
I have here two tables, I need to update both table row field from a post value. table1.item and table2.item update from post value. I didn't know how to set both table field equal to post value.
Any help will appreciate.
Table1
| pr_id | item |
----------------
| 1001 | pen |
Table2
| pr_id | item |
----------------
| 1001 | pen |
Query
<?php
$pr = $mysqli->real_escape_string($_POST["pr_id"]);
$item= $mysqli->real_escape_string($_POST["item"]);
$mysqli->query("UPDATE table1 LEFT JOIN table2 ON table1.pr_id = table2.pr_id WHERE table1 .pr_id = '$pr' ");
?>
Try this:
update table1 inner join table2
on table1.pr_id=table2.pr_id
//your changes
set table1.item ='some thing', table2.item ='some thing'
where table2 .pr_id ='$pr' ;
Try this:
Query:
UPDATE table1 T1 LEFT JOIN
table2 T2 ON T1.pr_id = T2.pr_id
SET T1.Item= '$item', T2.Item = '$item'
WHERE T1.pr_id = '$pr'
With php:
$mysqli->query("UPDATE table1 T1 LEFT JOIN table2 T2 ON T1.pr_id = T2.pr_id SET T1.Item= '$item', T2.Item = '$item' WHERE T1.pr_id = '$pr' ");
According to my opinion it is better to use Compound Trigger for updating 2 diffenent tables at the same time. Try to use that.
I have the following table structures.
Table A
id name
1 name1
2 name2
Table B
a_id b_id
1 1
1 2
How can I select all rows of table A that have both a b_id of 1 and 2? Table B is a mapping table between table A and another table, whose contents do not matter for this question.
Thank you for your time and help!
This query uses COUNT(DISTINCT) to ensure the presence of both values. If I did not use DISTINCT it may incorrectly count rows in TableB that look like this as a match when it shouldn't:
a_id b_id
1 1
1 1
select a.id, a.name
from TableA a
inner join (
select a_id
from TableB
where b_id in (1, 2)
group by a_id
having count(distinct b_id) = 2 #this number matches no. of unique values in IN clause
) b on a.id = b.a_id
SQL Fiddle example
Correctness can be tricky on a question like this because your sample data is missing a key cases. Duplicate values for B_ID and the possibility that it can contain one of the ids but not both
e.g.
| A_ID | B_ID |
---------------
| 1 | 1 |
| 1 | 2 |
| 2 | 1 |
| 2 | 1 |
The best approach is to use Having (Distinct Count) = # of ids (RedFilter's) since its easy to add more ID's
The two other options are to use or multiple EXISTS or IN clauses (NickB's) or to join and filter multiple times (below) but can become tortuously long if you need to add additional ids.
SELECT DISTINCT a.id,
a.name
FROM TableA a
INNER JOIN TableB b1
ON a.id = b1.a_id
and b1.b_id = 1
INNER JOIN TableB b2
ON a.id = b2.a_id
and b2.b_id = 2
DEMO
SELECT * FROM A JOIN B ON A.id=B.a_id WHERE B.b_id IN(1,2);
Here's what I could come up with, it uses one subquery.
SELECT * FROM table_a a1
JOIN table_b b1
ON a1.id = b1.a_id
WHERE b1.b_id = 1 AND
EXISTS(
SELECT b2.b_id
FROM table_b b2
WHERE a1.id = b2.a_id
AND b2.b_id = 2
)
Didn't know SQL Fiddle exists, but here is one showing it working!
I'll take a stab at this too, with a self join:
SELECT A.* FROM B B1
JOIN B B2 ON B2.a_id = B1.a_id
JOIN A ON A.id = B1.a_id
WHERE B1.b_id = 1 AND B2.b_id = 2
I tested this, and it works. If (B.a_id, B.b_id) isn't unique, then you'll need DISTINCT to avoid duplicates.
SELECT TableA.* FROM TableA WHERE TableA.id IN(
SELECT TableB.a_id FROM TableB WHERE TableB.b_id IN(1,2))
I have multiple tables in my database. Let's say the table users looks like this:
Users:
|id|name|gender|access|id_ext|
|1 | a | m | 1 | 32 |
|3 | b | m | 3 | 33 |
|4 | c | m | 1 | 34 |
|5 | d | f | 1 | 35 |
I would like to select the user with for example id_ext = 32 and then run another select statement using that selected users fields.
I can solve this by first getting the user with a query and then create another query with users info, but there must be a way to do this in the same query?
This is the query i use now:
SELECT * FROM users NATURAL JOIN
(SELECT id FROM ages WHERE age BETWEEN
(SELECT limit_age_l FROM users WHERE id=17)
AND (SELECT limit_age_h FROM users WHERE id=17)) as a
WHERE NOT id = 17
AND locale = 'en_US'
AND limit_gender = 1
AND visible = 0
AND NOT EXISTS (SELECT view_id FROM matches WHERE user_id = 17 AND view_id = a.id)
LIMIT 1
Problem is that the values id=17, limit_gender=1 and locale = 'en_US' in the query are not known. These are taken from the user with id_ext = '32'.
SELECT * FROM Users WHERE id in (SELECT id FROM Users WHERE id_ext='32');
Yes - assuming your subsequnt query is of the form:
select field1, field2, ...
from Table1
join Table2 on ...
where ...
and Table1.id = N /* previously selected id from users */
Then either by using the first query as a subquery:
select field1, field2, ...
from Table1
join Table2 on ...
where ...
and Table1.id = (select id from users where id_ext ='32')
/* replace = with IN if more than one id will be returned */
Or by joining to the results of the first query as part of the subsequent query:
select field1, field2, ...
from users
join Table1 on Table1.id = users.id
join Table2 on ...
where ...
and users.id_ext ='32'
(Note that both of these forms assume that users is not already being joined in the existing query - if it is, just add the users.id_ext ='32' condition to the existing query.)
EDIT: If I have understood the requirements correctly, the required query could be written as:
SELECT u.*
FROM users u
join ages a on u.id = a.id and
u.age between limit_age_l and limit_age_h
join users ul on ul.id = 17 and
ul.id <> u.id and
ul.locale = u.locale and
ul.limit_gender = u.limit_gender and
ul.visible = u.visible
AND NOT EXISTS (SELECT NULL
FROM matches m
WHERE m.user_id = ul.user_id AND m.view_id = a.id)
LIMIT 1
SELECT * FROM users WHERE id = (SELECT id FROM users WHERE id_ext = '32');
Select * from users as user inner join userinfo as usinfo on usinfo.id=user.id_ext where user.id_ext='32'
table1 (ids always exist)
+----+------+
| id | col1 |
+----+------+
| 1 | ab |
+----+------+
| 2 | gh |
+----+------+
table2 (ids always exist)
+----+------+
| id | col2 |
+----+------+
| 1 | cd |
+----+------+
| 2 | ij |
+----+------+
table3 (ids might be missing, in this case 2 is missing)
+----+------+
| id | col3 |
+----+------+
| 1 | ef |
+----+------+
PHP
$col = 'ab';
$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2, table3 AS t3
WHERE t1.id = t2.id AND t2.id = t3.id AND (t1.col1 = '$col' OR t2.col2 = '$col'
OR t3.col3 = '$col) GROUP BY t1.id, t2.id, t3.id");
That would only work if all three tables had "the same id" included, but what happens if an "id" is missing in table3 for whatever reason? how can I still test for all three tables and get t1.id to output 1, when $col = ab? would I have to use left join?
$a = mysql_query("SELECT t1.id FROM table1 AS t1, table2 AS t2
LEFT JOIN (SELECT id FROM table3 WHERE col3 = '$col') AS t3 ON t3.id = t1.id
WHERE t1.id = t2.id AND (t1.col1 = '$col' OR t2.col2 = '$col')
GROUP BY t1.id, t2.id");
what am I doing wrong here?
What are you doing wrong? Querying a table that doesn't exist. That's always going to raise an error.
I'm not going to address the wisdom of designing a database in which tables crucial to your queries come and go.
Your only hope on the client side is to
test for the existence of the tables
you're interested in, and
execute different SQL statements
based on those results.
[After your edit]
It sounds like you need either one or two left outer joins. This gives you all the ids that are common to both table1 and table2, regardless of whether they're in table3.
select t1.id, t2.id, t3.id
from table1 t1
inner join table2 t2 on (t1.id = t2.id)
left join table3 t3 on (t1.id = t3.id);
And this gives you all the ids that are in table1, regardless of whether they're in table2 or table 3.
select t1.id, t2.id, t3.id
from table1 t1
left join table2 t2 on (t1.id = t2.id)
left join table3 t3 on (t1.id = t3.id);
And, of course, you can filter the results with your WHERE clause.
LEFT JOIN only works when the table DOES exist, but contains no data.
If you don't want to have to do stuff like Catcall suggested (check if the table exists, and use different SQL statements based on that...) you have to make sure that the table exists in the database, even if it's completely empty.
Under normal circumstances, you should have a certain degree of control over your application's database, so you should be able to make sure that the table is really there.
If this is really a big problem (like, that you can't be sure if someone deleted the table) you could check this every time you start your application: create the table if it doesn't exist.