Estou juntando três tabelas SQL no CodeIgniter. Consigo recuperar os dados de três tabelas. Mas tenho problemas para encontrar a id adequada.
Eu coloquei minhas três tabelas abaixo:
CREATE TABLE `books` (
`id` int(11) NOT NULL,
`book_name` varchar(200) NOT NULL,
`description` text NOT NULL,
`author` varchar(200) NOT NULL,
`publisher` varchar(200) NOT NULL,
`price` varchar(200) NOT NULL,
`quantity` int(11) NOT NULL,
`categoryId` int(11) NOT NULL,
`book_image` varchar(200) NOT NULL,
`create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`userId` int(11) NOT NULL,
`status` enum('1','0') NOT NULL DEFAULT '0' COMMENT '1 = published | 0 = unpublished'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `category` (
`id` int(11) NOT NULL,
`category` varchar(100) NOT NULL,
`description` text NOT NULL,
`tag` varchar(100) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `users` (
`id` int(11) NOT NULL,
`name` varchar(200) NOT NULL,
`contact` varchar(50) NOT NULL,
`email` varchar(100) NOT NULL,
`password` varchar(255) NOT NULL,
`address` varchar(500) NOT NULL,
`city` varchar(50) NOT NULL,
`type` varchar(20) NOT NULL DEFAULT 'U',
`createdate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
public function get_books($limit, $offset)
{
/*=== SQL join ===*/
$this->db->select('*');
$this->db->from('category');
$this->db->join('books', 'books.categoryId = category.id');
$this->db->join('users', 'books.userId = users.id'); #...Join three sql table
$this->db->order_by('books.id', 'DESC');
$this->db->where('books.status', '1');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result();
}
Agora obtenho a id dos usuários em minha tabela associada. Mas eu quero a id dos livros como a id principal. Como eu poderia resolver esse problema?
Como a resposta anterior , você não deve usar '*'
this. Você deve escrever a consulta selecionada manualmente, selecionar a coluna que deseja nas tabelas.
Para obter o id do livro como seu id desejado, você deve alterar o código do modelo conforme abaixo.
Código de modelo
public function get_books($limit, $offset)
{
$this->db->select('books.id, books.book_name, books.description, books.author, books.book_image, books.otherCol, category.category, users.name'); // You can add many more if you want.
$this->db->from('books');
$this->db->join('category', 'books.categoryId = category.id'); //2nd join
$this->db->join('users', 'books.userId = users.id'); //3rd join
$this->db->order_by('books.id', 'DESC');
$this->db->where('books.status', '1');
$this->db->limit($limit, $offset);
$query = $this->db->get();
return $query->result();
}
Acho que agora você obterá a identificação desejada na mesa de livros .
Este artigo é coletado da Internet.
Se houver alguma infração, entre em [email protected] Delete.
deixe-me dizer algumas palavras