I have a table called places which has 2 million records. In these records there is a column called city_name.
In another table I have 2 columns: city_name and city_id.
My aim is to search the table cities with the city_name from the table places and insert the corresponding city_id from the table cities into the table places.
I have tried to use the following:
UPDATE places
INNER JOIN cities USING (city_name)
SET places.city_id = cities.city_id
The problem with this is that it has worked but i am having random city_id's in the city_id field that does not match the city name.
I should also mention that there might not be a corresponding city name in the table cities, so i want to ignore records that do not exist.
Can anyone help please. I am using phpmyadmin
Thank you
If I understand your request correctly the solution would be
UPDATE places
SET places.city_id = (SELECT cities.city_id
FROM cities
WHERE cities.city_name = places.city_name)
Related
I have the main table WORDS
This table has all the word names stored in database.
Now I have a foreign table WORD_TYPES
This table stores the type of word related to main table WORDS.
Finally I have foreign table POPULAR_WORDS.
My problem is I am trying to get 2 word names from main WORDS table connected with its those 2 foreign tables WORD_TYPES & POPULAR_WORDS.
SELECT
WORDS.word_name as word1,
WORDS.word_name as word2,
from table WORDS, WORD_TYPES, POPULAR_WORDS
where WORDS.w_id = WORD_TYPES.w_id
and WORDS.w_id = POPULAR_WORDS.wi_id
ORDER BY WORDS.word_name
But this is not possible for me, I have tried everything. Because after I selected a word_name from main table WORDS related to WORD_TYPES, I am not able to select the second word related to POPULAR_WORDS table.
I guess we can select only one row from a query in this specified way.
I think what you're looking for is :
SELECT
w1.word_name as word1,
w2.word_name as word2,
FROM table WORDS w1, WORDS w2
JOIN WORD_TYPES wt ON wt.w_id = w1.w_id
JOIN POPULAR_WORDS pw ON pw.w_id = w2.w_id
Even though the final result you want to obtain is not very clear
I'm using codeigniter.
We have two mysql tables.
What I'm doing is taking all the details in the first table and feeding it to an html page to let all users to view it. But now I want to add some details from a second table related to a column in first table.
I mean, I have a column in first table call "Pending_on" in that column I have inserted some branch names
and in the second table I have inserted the contact details of those departments.
So, if we think in the first table fifth row pending_on column has the value "HR" then I want to display the telephone number of the HR department.
So, how can I write a query for this?
For your case, here is an example:
There are 2 tables named entry and subscription.
entry
field1 publisherId
field2 entry
subscription
field1 myId
field2 friendId
You can use a JOIN. In plain SQL:
SELECT e.publisherId, e.entry
FROM entry e
JOIN subscription s
ON s.friendId = e.publisherId
WHERE s.myId = 1234
this is the query i used to do my work
and thanks goes to MR. Yuvvaraj Bathrabagu
$this->db->select('*');
$this->db->from('new_users');
$this->db->join('contact', 'new_users.pending_on = contact.dep','left');
$query = $this->db->get();
$data = $query->result_array();
Let's take your example. You have a column name pending_on in your first table, and in your second table you have your department_name column. Find the below code for joining those two tables,
$this->db->select('firsttable.*,secondtable.contact_details');
$this->db->from('firsttable');
$this->db->join('secondtable', 'firsttable.pending_on = secondtable.department_name','left');
$query = $this->db->get();
So, The department_name in second table and pending_on in first table should have the same value as you mentioned "HR". And my suggestion is to have the id's of the table as reference instead of department names. Hope This helps.
If I have two tables in a MySQL database that both have a column called order_number, given an order_number value but not knowing which table it comes from how would I go about setting up a query that would return the name of the table it was found in?
I am particularly interested in the name of the table so I can set up subsequent updates to that table.
Also, I am using PHP for the handling of the query.
select "tableA" as tableName,order_number from tableA where order_number=5
UNION
select "tableB" as tableName,order_number from tableB where order_number=5;
I have a publications database and I need to fetch some information regarding the author. The author field is such that the authors have been lumped together in one field e.g if a book has two authors called Robert Ludlum and John Grisham, in the database it is saved as Ludlum, R.;Grisham,J.;
My application needs to spool information and retrieve data on books authored by a particular author if they click on their name. I am using this statement to retrieve the data
$select = "SELECT tblPublications.Title, tblPublications.Year FROM tblPublications WHERE tblPublications.Authors LIKE '%$sname%'";
$sname is a variable referring to the surname of the author. The problem arises if two authors share the same surname. however a workaround I am trying to implement is to get the applicationtake the surname, insert a comma, take the first name of a user and get the first letter then combine the result to a stringe and match them to each comma delimited value in the author field e.g if it is Grisham's books I am looking for I use *Grisham, J.* in my query.
Any Idea how to do this in PHP,MYSQL?
If it is possible to redesign the database, you should probably have an authors table and a book_authors table that relates books to authors, so that multiple authors can be associated with each book. Where is the Last Name coming from that the user clicks? Is it possible to have the link generated be LastName, First letter of first name? If so then you can probably change the link so it will include the first letter. But it is still possible to have two authors with the same last name and first letter of first name. So I think the best solution is to have an authors table and a Book_authors table and just store the author id as a hidden field and use that to retrieve the books by the selected author.
Your database design is incorrect, you have not normalized the data.
If you use like to search with leading wildcards, you will kill any chance of using an index.
Your only option to fix (if you want to keep the mistaken CSV data) is to convert the table to MyISAM format and put a FULLTEXT index on the authors field.
You can then search for an author using
SELECT fielda, b,c FROM table1 WHERE MATCH(authors) against ('$lastname')
See: http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
Of course a better option would be to normalize the database and create a separate table for authors with a link table.
TABLE books
------------
id primary key
other book data
TABLE authors
--------------
id primary key
lastname varchar (indexed)
other author data
TABLE author_book_link
----------------------
author_id
book_id
PRIMARY KEY ab (author_id, book_id)
Now you can query using very fast indexes using something like:
SELECT b.name, b.ISBN, a.name
FROM books b
INNER JOIN author_book_link ab ON (ab.book_id = b.id)
INNER JOIN author a ON (a.id = ab.author_id)
WHERE a.lastname = '$lastname'
It would entirely depend on what input you are getting from the user.
If the user just types a name, then there isn't much you can do (as there is no guarantee that they will enter it in a correct format for you to parse).
If you are getting them to type in a firstname and lastname however, something like this could be done:
<?php
$firstname = trim(mysql_real_escape_string($_GET['fname']));
$surname = trim(mysql_real_escape_string($_GET['sname']));
$firstletter = substr($_GET['fname'],0,1);
$sname = $surname.', '.$firstletter;
$select = "SELECT tblPublications.Title,
tblPublications.Year
FROM tblPublications
WHERE tblPublications.Authors LIKE '%$sname%'";
Lets say I have a table tilistings with a dozen columns, and about 2,000 rows there is one column cityname that has probably 50 different values in it. What I want to do, is search through the tilistings and create another table that just contains the 50 different values from cityname without duplicating any names....basically if cityname had the values a,b,a,c,b,b,d,a,a,d,d,c I would only want the new table to contain a,b,c. Is there a pre-built MySQL function to do so? Otherwise, just point me in the right direction to do this with PHP. I can create the table, just looking to populate it.
Or do it all in SQL, if you already have created a table named cities with a single column cityname:
INSERT INTO `cities` (`cityname`)
SELECT DISTINCT `cityname` FROM `tilistings`;
Or crate the table from the SELECT:
CREATE TABLE `cities`
SELECT DISTINCT `cityname` FROM `tilistings`;
You can get the unique city names by performing the following query:
SELECT DISTINCT cityname FROM tilistings
Then loop through those and INSERT them into your new table with PHP or INSERT INTO ... SELECT.