+---------+ +-----------+ +---------+
| USER | | USER_LANG | | LANG |
| id_user | | id_user | | id_lang |
| name | | id_lang | | name |
| | | years | | |
+---------+ +-----------+ +---------+
I want to write query for saving data from user and user_lang in database at same time...is there some insert join or what?
No there isn't. You can only select or delete from multiple tables at once.
If table structure would have been same; it would have been possible. But in your case you will have to use multiple queries. If you want to ensure integrity between table data then use Stored Procedures.
try this
INSERT INTO LANG (id_lang,name)
VALUES (SELECT ul.id_lang ,u.name
FROM `USER` u
INNER JOIN `USER_LANG` ul
ON u.id_user = ul.id_user
)
Related
I have the following tables in mysql:
Table A:
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sid | varchar(50) | YES | | NULL | |
| type | int(11) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
Table B:
+---------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| channel | varchar(20) | YES | | NULL | |
| sid | varchar(50) | YES | | NULL | |
| type | varchar(20) | YES | | NULL | |
+---------+-------------+------+-----+---------+-------+
I want to find the rows from A that have an entry in B with the same sid. I tried the following Join command:
SELECT A.sid FROM A join B on A.sid=B.sid;
This query never gives me the answer.
Tabe A has 465420 entries and table B has 291326 entries.
Why does it not work?
Are there too many entries?
Or does it have anything to do with the fact that I have no primary keys assigned?
Your query is fine. You would appear to need an index. I would suggest B(sid).
You can also write the query as:
select a.sid
from a
where exists (select 1 from b where a.sid = b.sid);
This will not affect performance -- unless there are lots of duplicates in b -- but it will eliminate issues caused by duplicates in b.
Try
SELECT A1.sid
FROM (select A.sid from A order by sid) A1
join (select B.sid from B order by sid) B1
on A1.sid=B1.sid;
Else above holds true. You need index.
I have multiple tables with this structure:
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| nick | char(25) | NO | PRI | NULL | |
| id | int(11) | NO | | NULL | |
| cooldown | datetime | NO | | NULL | |
+----------+----------+------+-----+---------+-------+
I would like to obtain all of the id fields from all of these tables given a nick. These tables (lets call them table1, table2, table3) may or may not be empty. What is the best way to do this in one query?
My desired output would look like:
+-------+
| id |
+-------+
| 15679 |
| 72620 |
+-------+
You need a UNION query:
SELECT id FROM table1 WHERE nick='nick'
UNION
SELECT id FROM table2 WHERE nick='nick'
UNION
SELECT id FROM table3 WHERE nick='nick'
UNION will only return unique rows, if you want your query to return duplicates you can use UNION ALL instead.
Select Id from table1 where nick=nick
UNION
Select Id from table2 where nick=nick
UNION
...
UNION
...
First off, I would like to say that if after reading the question, anyone has a suggestion on a more informative title for this question, please tell me, as I think mine is somewhat lacking, now, on to business...
Given this table structure:
+---------+-------------------------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------------------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| account | varchar(20) | YES | UNI | NULL | |
| domain | varchar(100) | YES | | NULL | |
| status | enum('FAILED','PENDING','COMPLETE') | YES | | NULL | |
+---------+-------------------------------------+------+-----+---------+----------------+
And this data:
+----+---------+------------------+----------+
| id | account | domain | status |
+----+---------+------------------+----------+
| 1 | jim | somedomain.com | COMPLETE |
| 2 | bob | somedomain.com | COMPLETE |
| 3 | joe | somedomain.com | COMPLETE |
| 4 | frank | otherdomain.com | COMPLETE |
| 5 | betty | otherdomain.com | PENDING |
| 6 | shirley | otherdomain.com | FAILED |
| 7 | tom | thirddomain.com | FAILED |
| 8 | lou | fourthdomain.com | COMPLETE |
+----+---------+------------------+----------+
I would like to select all domains which have a 'COMPLETE' status for all accounts (rows).
Any domains which have a row containing any value other then 'COMPLETE' for the status must not be returned.
So in the above example, My expected result would be:
+------------------+
| domain |
+------------------+
| somedomain.com |
| fourthdomain.com |
+------------------+
Obviously, I can achieve this by using a sub-query such as:
mysql> select distinct domain from test_table where status = 'complete' and domain not in (select distinct domain from test_table where status != 'complete');
+------------------+
| domain |
+------------------+
| somedomain.com |
| fourthdomain.com |
+------------------+
2 rows in set (0.00 sec)
This will work fine on our little mock-up test table, but in the real situation, the tables in question will be tens (or even hundreds) of thousands of rows, and I'm curious if there is some more efficient way to do this, as the sub-query is slow and intensive.
How about this:
select domain
from test_table
group by domain
having sum(case when status = 'COMPLETE'
then 0 else 1 end) = 0
I think this will work. Effectively just joins two basic queries together, then compares their count.
select
main.domain
from
your_table main
inner join
(
select
domain, count(id) as cnt
from
your_table
where
status = 'complete'
group by
domain
) complete
on complete.domain = main.domain
group by
main.domain
having
count(main.id) = complete.cnt
You should also ensure you have an index on domain as this relies on a join on that column.
I have a complicated query to resolve and I don't know how to get the correct results. First of all, let me show you the tables I have:
+------------+
| users |
+------------+
| id |
| first_name |
| last_name |
+------------+
+--------------+
| clients |
+--------------+
| id |
| users_id |
| uid |
| access_token |
+--------------+
+---------------+
| users_follows |
+---------------+
| users_id |
| follow_id |
+---------------+
+-------------------+
| users_connections |
+-------------------+
| id |
| users_id |
| clients_id |
| uid |
| name |
+-------------------+
Our website uses Facebook Connect, so EACH user connected has a client UID (Facebook UID). For a functionality of the website I need a query that does the next: select all the the people you follow (users_id=ME) plus the users_connections but if a users_connection is also someone I follow do not include it on the final result. Finally, for this rows if it's a "following" I need users.first_name and users.last_name and if it's a connection NOT registered on our website I need users_connections.name.
I will have a lot of rows an probably people can have a lot of people who follow so a NOT IN and id's concatenated I think that it's not the best way to scale it.
Can anyone bring me some light?
Thank you in advance!
i want to do one thing on my mysql DB.
I have two tables:
first_table:
| article | longtext | YES | | NULL | |
| article_id | int(11) | YES | | NULL | |
Second_table:
| id | int(11) |
| name | longtext |
| commit_date | date |
When first_table.article_id = second_table.id, then article = name.
I want to select article and article_id from first table and sort them by commit_date (correct for every record), which is in the second table.
How to do it?
Take a look at the following query, should do what you want :
SELECT article, article_id
FROM first_table f
JOIN Second_table s ON f.article_id = s.id AND f.article = s.name
ORDER BY s.commit_date